Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between returning an object vs a function in a directive definition?

What is the functional difference between the following code (in Widget Uno) using a directive definition object (I think it's called..?)...

angular.module("app").     directive("widgetUno", ["$http", function ($http) {         return {                 // A whole bunch of crap going on here             },             templateUrl: "widgetUno.html"         };     }]); 

...And this code in Widget Dos?

angular.module("app").directive('widgetDos', function($http) {     return function(scope, element, attrs) {         // A whole bunch of crap going on here     }; }); 

I'm trying to convert a directive that's like Widget Uno into Widget Dos, but where do I reference the templateUrl? Is this possible in Widget Dos?

like image 873
David Rivers Avatar asked Aug 20 '13 20:08

David Rivers


People also ask

What is directive definition object?

The Directive Definition Object (DDO) tells the compiler how a Directive needs to be assembled. Common properties include the link function, controller function, restrict, template, and templateUrl.

What is the difference between the scope of a directive and the scope of a controller?

No difference. It is same object.

What is a functional directive?

The simplest way to think about a directive is that it is simply a function that we run on a particular DOM element. The directive function can provide extra functionality on the element.

What is the difference between controller and link in directives?

Answer:The link option is just a shortcut to setting up a post-link function. controller: The directive controller can be passed to another directive linking/compiling phase. It can be injected into other directices as a mean to use in inter-directive communication.


1 Answers

Returning only a function in a directive is just a shorthand for the link function in the full definition.

If you are specifying something other than a link function (like templateUrl) then you need to write it the long way:

angular.module("app").     directive("widgetUno", ["$http", function ($http) {         return {           link: function(scope, element, attrs) {              // A whole bunch of crap going on here           },           templateUrl: "widgetUno.html"         };     }]); 

This difference is actually documented here - http://docs.angularjs.org/guide/directive

like image 121
Terry Avatar answered Oct 26 '22 03:10

Terry