+ 5
Help with angular
I have trouble understanding the link function in making custom directives in angular js. Can someone explain them?
2 Answers
+ 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.