Practical Guide to PreLink, PostLink and Controller Methods of Angular Directives
Link function of an Angular Directive As the name implicates, the link function has the duty of linking the model to the templates. Link function is the place where AngularJs does the data binding to the compiled templates. Let’s take a look at the signature of a link function. link : function LinkFn ( scope , elem , attr , ctrl ) { } There are 4 parameters available to the link function. scope : The scope of the directive elem : Dom element where the directive is applied attr : Collection of attributes of the Dom Element ctrl : Array of controllers required by the directive Now let’s create a simple directive to see how the data binding works. See the JSFiddle below: The name and greeting properties attached to the scope are linked to the template once the link function is executed. And, the browser will show “Hey, I am Paul” in the view. The above is the usual way to create a link fu...