What the return function does in a directive declaration

I am learning about creating directive in AngularJS.

The official docs say:

Best Practice: Prefer using the definition object over returning a function.

      

But he never gives an example of a function return. It always gives examples that return a definition object.

Question . What does the directive do when you return a function instead of a definition object?

+3


source to share


1 answer


Yes, the directive documentation doesn't say anything about returning a function.

But, this $ compile documention says:

API overarching directive

There are many different options for the directive.

The difference lies in the return value of the factory function. You can either return a "Directive Definition Object" (see below) defining the directive's properties, or just a function postLink

(all other properties will have their default values).



And an example is given below (see comment below).

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) { ... }
});

      

+2


source







All Articles