matthewtyler.io

Angular.js Part 2: A little bit of directive

September 03, 2013

Hi guys,

I’ve finally got around to my next post in my series of angular posts.

In this instance, we’re going to convert the fieldtracker app we made last week into a self-contained directive. An directive (in the angularjs world) is a way of defining a self-contained component that is ideally reusable. Now, what I’m going to show isn’t going to be the best way of doing things - but it will get you moving in the right direction to using directives. There will be some follow up resources at the end of the post.

As a note, directives are also the preferred way of doing DOM manipulation, so if you need to use jquery for anything its preferred that you do in this in the directive, and not in the controller. The reason for this is simple - it’s not guaranteed the DOM has been prepared when the controller code runs, so controller based DOM manipulation may fail.

Simple enough. Let’s move onto our directive.js file. First, we need to define a module. An angular module contains a set of dependent components, be they directives, filters, services etc. You might remember that we defined a module in the previous tutorial ‘myApp’ which we used with ng-app (check back up there in the layout.jade file, you’ll see it) to bootstrap the angular application and provide the app dependencies. Indeed, our app.js file looks this-

Here, we can see we are defining a module myApp, which depends on myApp.* - which themselves are all modules. It helps to think of an angular application as a set of component modules, which we provide to other modules ad nauseum until they eventually hit our main app module.

Here we define a module ‘myApp.directives’. The empty square brackets indicate that we have no dependencies. As an aside, if we gave a dependency that did not exist in our project because we have not included it, angularjs would report a dependency not found error to the console. Feel free to try it out!

We have define one directive inline with our module definition called appVersion - this simply depends on the version api provided in angularjs to return version information about angularjs, which it then uses to return inside the text element of the directive. ie, if somewhere on my page I did

span(app version)

I would expect it to print the angularjs version information.

Our fieldtracker is going to be relatively simple. Lets look at the directive definition

We define the directive much like we did previously with appVersion. This time though, our return statement is a little more complex. It should be noted that there are a boatload of ways to define directives, and it’s likely as you look over different angular projects you’ll see a lot of different methods. In this instance, we a directly returning what angularjs calls a ‘directive definition object’ which is essentially the DNA of our directive. We return an object that consists of several different fields.

The following is information on the ones we can see in this particular directive.

restrict - this determines how declare our directive when used in our views. There are 4 main types, element, attribute, class, and comment. The latter one is rarely used. As an example of each type is declared as thus-

You can choose to ‘restrict’ you directive to only work when declared as any or all of them. What you pick will depend on personal circumstance. I have noted that specific kinds of DOM manipulation will complain if you do not have a root element. In this instance, you can get around this by using class and attribute type directives.

replace - This determines whether the output of the directive replaces the the tag in which it was defined, or gets set as an element underneath it

example:
if false -

<div some-directive>
<!– some directive contents –>
</div>

if true -

<!– some directive contents –>

controller - This is a way of passing in a controller for the directive to use. If methods are defined on the controller, they can be called from the linking function. Likewise, the scope of the controller will act on the scope of the directive. You can of course, do what you would in a controller in the linking function of the directive (in fact, many do), but this is perhaps a better way of decoupling the listeners from the view. You could also share the controller logic with multiple directives, if neccesary.

scope - This is where things can get a bit tricky. If you do not include this variable, the scope of the directive will be the same as it’s parent scope (ie the scope of wherever you defined the directive). Otherwise, the as soon as it’s declared the behaviour will be to create for the directive it’s own scope. This directive scope is a child of the scope on which the directive was defined. Generally, I’d consider it good practice to declare a scope for a directive, as this decouples it from it’s parent scope and it promotes component reuse. You can however, still access the parent scope by scope.parent.

There are a few other points about scope that need to be mentioned. By adding variables to the scope, you declare that you expect these to be passed in as attributes to the directive. Imagine we had-

we would then expect something like the following definition in directive definition object:

scope: {
field1: '=',
field2: '@',
field3: '&'
}

The values of those fields are significant.

= - implies double binding. If the value changes in the directive or from where it was passed in either, it will be reflected to the other variable. So if you had defined some variable on the parent scope and passed it to the directive, if the directive changes it’s value at some point that change will be reflected back.

@ - or ‘string’ binding essentially passes the string value of the variable to the directive. Note this essentially means there is no two way binding occurring here. The parent and the parent alone is capable of changing the value passed to the directive. Note that unlike the previous type, if you want binding you need to explicitly state it with curly brackets ie ‘{{field2}}’ and not ‘field2’ as printed in the example, because this will literally assume field2 is the value that you want to pass in.

& - is expression scope and is used a bit less than the other two. It is a way of passing a function to the directive so that it evaluates on the parent scope. For example, say were building a calculator application and enter a number on in a text box. Clicking a button on the calculator increments the number, but you want this number passed back up to the parent scope - or you want the parent scope to define what function is called when a particular button is pressed. This is a manner in which you could achieve this functionality.

In my fieldtracker directive I haven’t used any of these, but I thought it would be a good idea to cover.

templateUrl - This is a way of passing in a template to the directive by (obviously) url. In mine I’m passing a partial which will passed into the element of the directive on compile/link. By doing this, you don’t have to explicitly create each DOM element in the compile phase of the directive. You can similarly use the ‘template’ parameter if you feel like explicitly writing out the template inline with the directive.

Ok now on to the meaty bit of directives, the dreaded compile/link phase.

The first phase, compile, deals with transforming the template DOM. A lot of directives won’t and don’t need to do this. ngRepeat and ngView are directives that do, if you are looking for examples.

A compile function is required any time a change in the model results in a change of DOM structure. A good example is using an ngRepeat directive with a list, and the list suddenly loses or gains an item. You expect this change to reflected in the ngRepeat list.

But, we expect to have a reasonably amount of performance when doing this. Let’s imagine we had other directives as child elements of the ngRepeat tag. If we added another element to our list, these would all need to be compiled and relinked again. In ngRepeats case, it prevents the compilation process from descending down into elements, and instead provides a link function, which can copied and provided whenever a new element is added to the list.

Which brings us to the link function. The link function allows use to register listeners on specific DOM elements, as well as copy content from the DOM into the scope. You often find controller-type logic in the linking function.

Don’t use this as the definitive guide to the compile/link phase. I highly recommend looking at code samples around the web and reading various articles on it. It’s very hard to get a good grasp on this without reading code and experimenting!

One last note, is that compile function always returns either a linking function, or an object with attributes labelled pre and post (which are themselves linking functions). If you do not have a compile function, you can omit it and simply have a ‘link’ attribute on the directive definition object. Pre and Post, determine at what stage of the linking process you want your code to execute at. Postlinking is the most common, and the main difference between them is that it is unsafe to do DOM manipulation in the prelink function.

In my code, I haven’t used anything in the compile/link phase, although I could place the controller code inside the link function and it would still retain it’s functionality.

There is only two more things we need to do. First we move our template code to partial/3, as defined in the directive.

Now all we have to do is use the below code and our directive will function just as normal!

div(fieldtracker)

Obviously, there is nothing too ‘reusable’ about this example. All it does is let me (very easily) put the fieldtracker widget with the same backend anywhere I want it. Even if I placed two on the same page, changing one would not update the other, as each has its own instance of the controller. You could potentially implement shared functionally a few different ways, but essentially you’d need a way to inform other fieldtrackers that what they are displaying needs to change.

What things could we change to make fieldtracker more flexible? We could
- Give the fieldtracker an attribute so we can define it like

and pass the api attribute to the controller, so we can change what REST endpoint to use

Replace the controller with a singleton service, so all instances are updated appropriately when a change is made on one of them. This could be done a few different ways; by $watch-ing the variable on the service in the directive and updating the appropriate entry when the variable changes (or refreshing the whole directive, your call!). Perhaps we use a websocket and update the view when the server changes the backend (pub/sub) ?

Modify the controller slightly so that rather having the functions defined on the scope, they are returned as set of functions on the controller, then setting them up on the scope in the linking function. This could potentially make the controller reusable in another (yet unwritten) directive.

To wrap this up, when writing directives you essentially need to consider -

What is the template of the directive going to be?

How am I going to pass data into this directive? by attribute, by service?

Do I need a controller? and am I going to have sub-directives that are going to need access to this controller?

What attributes do I need to watch for changes?

Do I need/want to transclude any content?

Am I going to need to do any Jquery style DOM manipulation?

Writing directives aren’t a whole lot different then writing views and wiring up controllers, so there is not anything to fear.

As always, if you want more information on directives, the following are fantastic sources of information;