function MyDirective(){
var ddo = {
scope: {...},
link: linkFunction,
...
templateUrl: 'template.html'
};
return ddo;
}
function LinkFunction(scope, element, attrs, controller){
...
}
- DOM manipulation usually done inside of the link function declared on the DDO.
- Link function does not support injection. To use injected components, services, inject them into directive.
- 'scope' parameter is the exact
$scope
in directive's controller. - 'element' object represents the element of the directive:
- Top level element.
- It's jqLite object or jQuery object (if jQuery is included before Angular).
function MyDirective(){
var ddo = {
scope: {...},
transclude: true,
...
templateUrl: "template.html"
};
return ddo;
}
<my-directive>
<span>
WARNING! WARNING! {{ctrl.someProp}}
</span>
</my-directive>
{{ctrl.someProp}}
- evaluated in the parent controller, NOT in our directive
<div>
...
<div ng-transclude></div>
</div>
<div ng-transclude></div>
- insert evaluated wrapped content into element marked with ng-transclude
transclude
allows whole templates to be passed into a directive.- The wrapped content is evaluated in the parent's context, NOT in the directive's context.
- In the DDO:
- transclude: true
- In directive's template:
- ng-transclude attribute designates place for evaluated wrapped content.