In this article I will discuss Angular Directives and why they are near and dear to my heart
Directives
Directives in Angular are modular pieces of functionality which in some cases are very analogous to plugins in jQuery. According to the Angular documentation…
“At a high level, directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS’s HTML compiler ($compile
) to attach a specified behavior to that DOM element or even transform the DOM element and its children.”
For more on the directive documentation look here – https://docs.angularjs.org/guide/directive
I have already shown and demonstrated some of the core Angular directives like ngApp and ngView. Those attributes within the HTML are the instructions to “Do Something Here”
<div ng-app="personApp"> <div ng-view></div> </div>
The on-last-repeat directive
Within my demo application I wanted to know when the table was finished loading. The problem is in Angular that there is no “I am finished with the template load” event. The reason is due to the asynchronous nature of the Angular code.
I wanted to load the table and then take an action on it. To do this I added a directive to the “repeat control” within my template for loading people.
The following example was completely plagiarized from this article – respect !
http://www.nodewiz.biz/angular-js-final-callback-after-ng-repeat/
You can see the on-last-repeat attribute within the first TR
<tr ng-repeat="person in people" on-last-repeat> <td>{{person.firstname}}</td> <td>{{person.lastname}}</td> <td class="zipCode">{{person.zip}}</td> <td class="user">{{person.username}}</td> <td><a class="btn btn-info" href="#/person/{{person['@unid']}}">Edit</a></td> <td><a class="btn btn-warning" href="#/person/{{person['@unid']}}/delete">Delete</a></td> </tr>
I then created a directive within my app.js to do something with the on-last-repeat
personApp.directive('onLastRepeat', function() { return function(scope, element, attrs) { if (scope.$last) setTimeout(function(){ scope.$emit('onRepeatLast', element, attrs); }, 1); }; })
Within the code we are creating a custom “event” which can be listened for in another part of the application
- If the last entry is triggeredhere
then - $emit the onRepeatLast event within the application
Then in the PeopleListCtrl controller we set a listener –
- $on the onRepeatLast event
- Do something
peopleControllers.controller('PeopleListCtrl', ['$scope', '$http', 'peopleFactory', function ($scope, $http, peopleFactory) { $scope.$on('onRepeatLast', function(scope, element, attrs){ alert('The table has loaded') });
Well that is a little simple….
Yes it is but it serves to demonstrate the concept. The best part is that there are lots of people who are writing plug and play Directives for Angular – just like jQuery plugins. If I could be bothered I might even do some blog posts on useful Directives…..But is a good resource for looking at some available directives.
There are multiple directives for using jQuery plugins within an angular construct – this is very helpful for data binding. There are also multiple directives which are independent of jQuery and are stand along “Angular” plugins for want of a better term.
Here’s another good resource
http://ngmodules.org/
Thanks mate 🙂
TEST COMMENT
How is this different that using event callbacks. (My fourth attempt in posting this)
Currently there is no event for “template has finished loading”
I am a bit surpirse
Reblogged this on Dinesh Ram Kali..