Thursday, September 7, 2017

How to create custom directives in AngularJS?

You can create your own custom directive by using following syntax:



var app = angular.module('app', []);

//creating custom directive syntax
app.directive("myDir", function () {

return {

restrict: "E", //define directive type like E = element, A = attribute, C = class, M = comment

scope: { //create a new child scope or an isolate scope

title: '@' //@ reads the attribute value,
//= provides two-way binding,

//& works with functions
},

template: "<div>{{ myName }}</div>",// define HTML markup

templateUrl: 'mytemplate.html', //path to the template, used by the directive

replace: true | false, // replace original markup with template yes/no

transclude: true | false, // copy original HTML content yes/no


controller: function (scope) { //define controller, associated with the directive template

//TODO:

},


link: function (scope, element, attrs, controller) {//define function, used for DOM manipulation

//TODO:

}

}

});

No comments:

Post a Comment

What is restrict option in directive?

The restrict option in angular directive, is used to specify how a directive will be invoked in your angular app i.e. as an attribute, class...