+ 5
Help with angular
I have trouble understanding the link function in making custom directives in angular js. Can someone explain them?
2 ответов
+ 3
I found a really good tutorial : https://youtu.be/utKtjxLako4
+ 2
AngularJS Directive’s link key defines link function for the directive. Precisely, using link function, we can define directive’s API & functions that can then be used by directive to preform some business logic. The link function is also responsible for registering DOM listeners as well as updating the DOM. It is executed after the template has been cloned.
link function gets executed for each instance of directive so that each instance get’s it’s own business-logic without affecting others. If we need to add functionality to our instance of the directive, we can add it to the scope of the element we’re working with. AngularJS passes several arguments to the link function, which looks like following:
link: function(scope, element, attrs) {
...
}
scope : It is the scope of the element the directive is applied on. Note that scope is not injected but passed in.
element : It is the DOM element in HTML the directive is applied on.
attrs : These are the list of attributes as string available on the element in HTML.