Extending HTML with AngularJS’s Directives

The main thing about AngularJS is that it allows us to extend the capabilities of HTML to serve the purpose of today's dynamic webpages. In this article, I will show you how you can use AngularJS's Directives to make your development faster, easier, and your code more maintainable.

Preparation

Step 1: HTML Template

To make things simpler we will write all of our code in one HTML file. Create it and put the basic HTML template in it:

Now add the angular.min.js file from Google's CDN in the <head> of the document:

Step 2: Creating the Module

Now let's create the module for our directives. I will call it example, but you can choose whatever you want, just keep in mind that we will use this name as a namespace for the directives we create later. 

Put this code in a script tag a the bottom of the <head>:

We don't have any dependencies, so the array in the second argument of angular.module() is empty, but do not remove it completely or you will get a $injector:nomod error, because one-argument form of angular.module() retrieves a reference to the already existing module instead of creating a new one.

You also have to add a ng-app="example" attribute to the <body> tag for the app to work. After that the file should look like this:

The Attribute Directive: 1337 C0NV3R73R

First, we will create a simple directive that will work similarly to ngBind, but it will change the text to the leet speak.

Step 1: Directive Declaration

Directives are declared using module.directive() method:

The first argument is the name of the directive. It must be in camelCase, but since HTML is case-insensitive you will use dash-delimited lower-case form of it (example-bind-leet) in your HTML code.

The function passed as the second argument must return an object describing the directive. For now it will only have one property: The link function:

Step 2: The Link Function

You can either define the function before the return statement, or directly in the object that is returned. It is used to manipulate the DOM of the element that our directive was applied to and is called with three arguments:

$scope is an Angular scope object, $elem is the DOM element that this directive matched (it's wrapped in jqLite, AngularJS's subset of jQuery's most commonly used functions) and attrs is an object with all of the element's attributes (with normalized names, so example-bind-leet will be available as attrs.exampleBindLeet).

The simplest possible code for this function in our directive would look like this:

First, we replace some of the letters in the text provided in the example-bind-leet attribute with their replacements from the leet table. The table looks like this:

You should place it at the top of your <script> tag. As you can see this is the most basic leet converter as it replaces only ten characters.

Afterwards, we converted the string to leet speak we use jqLite's text() method to put it in the inner text of the element that this directive matched.

Now you can test it by putting this HTML code in the <body> of the document:

The output should look like this:

But this is not exactly how the ngBind directive works. We will change that in the next steps.

Step 3: The Scope

First of all, the thing passed in the example-bind-leet attribute should be a reference to a variable in the current scope, not the text that we want to convert. To do this we will have to create an isolated scope for the directive. 

We can achieve that by adding a scope object to the return value of our directive function:

Each property in this object will be available in directive's scope. It's value will be determined by the value here. If we use '-' the value will be equal to the value of the attribute with the same name as the property. Using '=' will tell the compiler that we expect a variable from the current scope to be passed - which will work just like ngBind:

You can also use anything as the property name, and put the normalized (converted to camelCase) attribute name after - or =:

Choose what works best for you. Now we also have to change the link function to use the $scope instead of attr:

Now use ngInit or create a controller and change the value of the div's example-bind-leet attribute to the name of the variable you used:

Step 4: Detecting Changes

But that is still not how ngBind works. To see that let's add an input field to change the value of textToConvert after the page has been loaded:

Now if you open the page and try to change text in the input you will see that nothing changes in our div. This is because the link() function is called once per directive at compilation time, so it can't change the content of the element every time something changes in the scope. 

To change that we will use the $scope.$watch() method. It accepts two parameters: first one is Angular expression which will be evaluated every time the scope is modified, second is a callback function that will be called when the expression's value has changed.

First, let's put the code we had in the link() function in a local function inside it:

Now after that function we will call $scope.$watch() like this:

If you open the page now and change something in the input field you will see that the content of our div also changes, as expected.

The Element Directive: Progress Bar

Now we will write a directive that will create a progress bar for us. To do that we will use a new element: <example-progress>.

Step 1: Style

To make our progress bar look like a progress bar we will have to use some CSS. Put this code in a <style> element in the <head> of the document:

As you can see it's pretty basic - we use a combination of position: relative and position: absolute to position the green bar and the value inside our <example-progress> element.

Step 2: Directive's Properties

This one will require few more options than the previous one. Take a look at this code (and insert it into your <script> tag):

As you can see we are still using a scope (with two properties this time - value for the current value and max for the maximum value) and the link() function, but there are two new properties:

  • restrict: 'E' - this one tells the compiler to look for elements instead of attributes. Possible values are:
    • 'A' - matches only attribute names (this is the default behavior, so you don't need to set it if you want to match only attributes)
    • 'E' - matches only element names
    • 'C' - matches only class names
  • You can combine them, for example 'AEC' would match attribute, element and class names.
  • template: '' - this allows us to change the inner HTML of our element (there is also templateUrl if you want to load your HTML from separate file)

Of course, we will not leave template blank. Put this HTML there:

As you can see we can also use Angluar expressions in the template - percentValue will be taken from directive's scope.

Step 3: The Link Function

This function will be similar to the one in previous directive. First, create a local function that will perform the directive's logic - in this case update the percentValue and set div.progressBar's width:

As you can see, we can't use .css() to change the div.progressBar's width because jqLite doesn't support selectors in .children(). We also need to use Math.min() and Math.max() to keep the value between 0% and 100% - Math.max() will return 0 if precentValue is lower than 0 and Math.min() will return 100 if percentValue is higher than 100.

Now instead of two $scope.$watch() calls (we have to watch for changes in $scope.value and $scope.max) let's use $scope.$watchCollection(), which is similar but works on collections of properties:

Note that we are passing string which looks like an array as the first paramter, not JavaScript's Array.

To see how it works first change ngInit to initialize two more variables:

And then add the <example-progress> element below the div we used earlier:

The <body> should look like this now:

And this is the result:

Step 4: Adding Animations Using jQuery

If you add inputs for progressValue and progressMax like this:

You will notice that when you change any of the values the change in width is immediate. To make it look a bit nicer let's use jQuery to animate it. The nice thing about using jQuery with AngularJS is that when you include jQuery's <script> Angular will automatically replace jqLite with it, making $elem a jQuery object.

So let's begin with adding the jQuery script to the <head> of the document, before AngularJS:

Now we can change our updateProgress() function to use jQuery's .animate() method. Change this line:

To this:

And you should have a beautifully animated progress bar. We had to use .stop() method to stop and finish any pending animations in case we change any value while the animation is in progress (try to remove it and change the values in inputs rapidly to see why it was needed).

Of course you should change the CSS and probably use some other easing function in your app to match your style.

Conclusion

AngularJS's directives are a powerful tool for any web developer. You can create a set of your own directives to simplify and boost your developing process. What you can create is only limited by your imagination, you can pretty much convert all of your server-side templates to AngularJS directives.

Useful Links

Here are some links to AngularJS documentation:

Tags:

Comments

Related Articles