Restriction and Transclusion Properties Of AngularJS Directives
Restriction and Transclusion are the properties of AngularJS directives. Restriction property is used to defined the use of directives where as Transclusion property is used to define the place for external content.
Restriction in Directives :
We can define directives in HTML in following ways:
As an Element (E)
Example :
As an Attribute of an element (A)
Example :
As a CSS class (C)
Example :
As a Comment (M)
Example :
We can define Restriction property of directive in a following way :
app.directive('myDirective', function () {
return { restrict: 'EA', scope: { }
};
});
Element (E) and Attributes (A) directives are most widely used where as Class (C) and Comment (C) directives are rarely used.
Transclusion in directives:
To support Transclusion AngularJS provides two key features :
1) transclude : It’s directives property and it’s need to set to true to support transclusion
2) ng-transclude : ng-transclude itself is a directive, placed inside the another directive template and it define the place where external content will get placed.
2) ng-transclude : ng-transclude itself is a directive, placed inside the another directive template and it define the place where external content will get placed.
Example Without Transclusion:
index.html
<my-directive title=”Transclusion Demo”>Content inside the custom directive</my-directive>
index.js
return { restrict: 'EA',
scope:
{ title: '@'
},
template: '<div><div>{{title}}</div><br></div>'
};
});
Output:
Example With Transclusion:
index.html
<my-directive title=”Transclusion Demo”>Content inside the custom directive</my-directive>
index.js
return {
restrict: 'EA',
transclude: true,
scope: {
title: '@'
},
template: '<div><div>{{title}}</div><br><div ng-transclude></div></div>'
};
});
Output: