Reputation: 12333
I'm learning about directive creation in AngularJS.
In the official docs, it says:
Best Practice: Prefer using the definition object over returning a function.
But it never gives an example of returning a function. It always gives examples returning a definition object.
Question: What does a directive do when you return a function instead of a definition object?
Upvotes: 3
Views: 530
Reputation: 11547
Yeah, the directive documentation doesn't mention anything about returning a function.
But, this $compile documention does say:
Comprehensive Directive API
There are many different options for a directive.
The difference resides in the return value of the factory function. You can either return a "Directive Definition Object" (see below) that defines the directive properties, or just the
postLink
function (all other properties will have the default values).
And there is an example below (see the comment at the bottom).
var myModule = angular.module(...);
myModule.directive('directiveName', function factory(injectables) {
var directiveDefinitionObject = {
link: function postLink(scope, iElement, iAttrs) { ... }
};
return directiveDefinitionObject;
// or
// return function postLink(scope, iElement, iAttrs) { ... }
});
Upvotes: 2