Angular Prime Directive

On my journey through the #RIMdev team, I’ve been maintaining a lot of Angular 1.x . I’ve had to learn through many mistakes, errors, and other “face-palm” moments. My relationship with the framework has been somewhat of a rollercoaster. I enjoyed every bit of it, don’t get me wrong, but certain quirks seemed to keep laughing at me…

After some time, I was able to help with most of our maintenance needs. One concept that I never understood, was the directive, though. We had some in our code base, but I had never had to touch them. They sat there, working away. Until that day came: I had to fix one.
I had a new mentor to help me jump in feet first, and understand them.

Why are they so confusing?

I think I was not alone. Many beginners in the Angular world ask similar questions: What are directives? How do they help, I’ve lived without them so far? Why do you need to bring in a new concept? Directives are simple: they are a way to create custom HTML syntax that works for your specific application. The benefit? It groups small portions of your view with common functionality into a unit. Angular compiles your custom HTML and attaches functionality to the DOM node. So in short, it’s some custom HTML. It breaks our application into smaller, more maintainable components (definitely not a new concept). So let’s get to some code.

Imagine we start with the following index.html, which produces a list of animals and counts how many times they speak:

That’s a lot of fuddled HTML in one file, which makes it hard to understand. Also imagine, this small list we are building is just one small part of our large app. What if we need to use this same type of list on a different page? We’d have to rewrite the HTML from scratch. Let’s start refactoring our list of animals into a reusable directive.

First, let’s create a new file animal-list-directive.html, and pull the following content out of our index.html:

<h1>Animals</h1><ulclass="list-group"><ling-repeat="animal in animals"class="list-group-item"ng-click="speak(animal);"><h4class="list-group-item-heading"></h4><pclass="list-group-item-text"><spanclass="badge"></span></p></li></ul>

In our app.js, we can add a directive:

...angular.module('app',[]).directive('animalList',function(){//We return a configuration objectreturn{//we tell it where the partial istemplateUrl:'animal-list-directive.html',//we want to use this like an (E)lement: also available (A)ttribute (C)lassrestrict:'E',//we'll be able to control this partial from this controllercontroller:function($scope){//move the speak function into the directive's controller$scope.speak=function(animal){animal.count++;}}}});...

And back in the body of index.html, we replace the HTML with the directive:

Tip: Notice the name of the directive is camelCase and the element is kebab-case like most HTML elements

Well this works! And, I must say, our HTML looks small and easy to manage. What’s wrong with this code, though? The problem is: we are dependent on the parent $scope. We are accessing the animals from the parent controller, inside our directive. Well that stinks, we were supposed to encapsulate our handiwork. What if we were to to declare a variable on $scope inside our directive AND inside our controller with the same name? Our directive would overwrite whatever was in the controller (since it is compiled and instantiated last). This scope leakage is an easy trap to fall into. There are 3 values you can use to specify scope in the configuration object of your directive:

false (Equivalent to not specifying a scope): It will use its parent’s scope

true : It will create a new scope that inherits from the parent scope

{} (Object literal) : An isolate scope, a new scope that doesn’t inherit from the parent

We are going to use an isolate scope, which in most cases, gives you the best encapsulation. Adding this to our directive in app.js:

scope: { listOfAnimals: '=' } tells Angular we are now expecting data from outside our directive to come into our directive. As you can see, we use an '=' sign to signify we want 2-way data-binding to a model. Use this prefix and Angular will always expect a model on the other end. You have 2 more options to use:

'&' : Method-binding. Angular expects a function on the parent scope to bind to

So, we isolated our scope, but how do we pass in the data we need? We’ll need to modify our index.html, and pass our animals from our parent scope :

...
<divclass="container"ng-app="app"><divclass="row"ng-controller="appCtrl"><divclass="col-md-12"><!-- Notice the kebab-cased attribute, that was camelCase in the directive --><animal-listlist-of-animals="animals"></animal-list></div></div></div>
...

<h1>Animals</h1><ulclass="list-group"><ling-repeat="animal in listOfAnimals"class="list-group-item"ng-click="speak(animal);"><h4class="list-group-item-heading"></h4><pclass="list-group-item-text"><spanclass="badge"></span></p></li></ul>

And viola! We have successfully refactored our animal list into a directive. We could then pick easily add an <animal-list> element anywhere we want!

The Angular Prime Directive? Make prime Angular directives.

Note: Obviously we could have just declared the animals inside of the directive for our simple use here, but now we can use any list of animals from any other data sets we have, and aren’t limited to the declared set in the directive