Component in AngularJS

15 / Apr / 2014 by Amit Kumar 0 comments

When we are working on front-end part, then many a times we face a situation where same html repeats on same page or different pages, at that time we think of making that html a component. When using AngularJS framework, we can easily make components with the help of directives. Let’s quickly understand what a directive is?
Directive is a marker on DOM element, which tells AngularJS to bind a specified behavior to the DOM element and its children elements. AngularJS itself provides many built-in directives e.g. ngRepeat, ngShow, ngModel etc. AngularJS is case-sensitive while html is case-insensitive, so defining a directive with AngularJS and using a directive with html have a slightly different signature. Let’s see a very commonly used directive ngModel:


We have marked ngModel directive to DOM element (input). We are using directive on html as ‘ng-model‘ (small letters with dash/minus as delimiter for camelCase words) while its actually defined with AngularJS as ngModel. Now let’s create a simple custom directive (We define a directory to register it with module via module.directive API).

var directiveTestApp = angular.module('directiveTest', []);
directiveTestApp.directive('myTemplate', function () { // Registering myTemplate directive to directiveTest module
    return {
        /*
         * Restricting a directive by providing a restrict field so that it can  be used as:
         * E stands for Element
         * C stand for Class name
         * M stands for Comment
         * A stands for Attribute
         * Here we are defining directive with all the types, if we don't provide restrict field then default value is
         * 'A' means directive can be used as attribute only.
         * */
        restrict: "ECMA", // directive can be used as attribute, element, class or comment.
        replace: true, // replace indicates that we want to replace whole div or only inner HTML, default value is false.
        template: "<div>this is directive template test</div>" // DOM element should be replaced with this.
        // If you want to provide template content from HTML file use templateUrl option instead of template
    }; 
});

To define a directive, we use module.directive API, in which first argument is name of directive, and second argument is function which returns an object with all the options. This function is called at the time of initializing the directive. Therefore this function is also know as factory function. For the first time when AngularJS finds the use of directive, it calls this factory function and stores the results for future use.

We can use directive on html as attribute, tag element, class name and comment as below:

1. Attribute Directive: <div></div> <hr />
2. Element Directive:  <hr />
3. Class Directive: <div class="my-template"></div> <hr />
4. Comment Directive: <!-- directive: my-template hello --> <hr />

NOTE: You can checkout full working source code from this link.

Amit Kumar
amit.kumar@intelligrape.com
in.linkedin.com/in/amitkumar0110
twitter.com/amit_kumar0110
More Blogs by Me

FOUND THIS USEFUL? SHARE IT

Leave a comment -