Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AngularJS directive pre-link and post-link functions be customized?

I have seen many references to AngularJS pre- and post-link functions in literature about AngularJS.

I am not sure however whether these can be customized or are internal to the framework.

In other words, as an AngularJS developper, can I provide my own pre and post link functions to my custom directives?

like image 879
balteo Avatar asked Feb 28 '14 20:02

balteo


People also ask

Can we create custom directive in AngularJS?

Angular provides the ability to create custom directives. A directive is basically a custom HTML attribute. It is a way to extend HTML vocabulary and modify the behavior of the DOM and your Angular application.

What are compile pre and post linking in AngularJS?

Pre-linking function Executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking. Post-linking function Executed after the child elements are linked.

What is Link function in AngularJS directive?

link function is basically used to manipulate the DOM( Document Object Model ) element using custom directive. link option in custom directive registers DOM listener and also update the DOM.

How many types of directives are there in AngularJS?

Types of Directive in AngularJS There are two types of AngularJs directives: Built-in directive.


1 Answers

Yes you can, as per @Mikke's answer. To sum up, there are four ways to declare linking functions:

  1. From within compile specifying both preLink and postLink functions explicitly:

    compile: function compile(tElement, tAttrs, transclude) {
      return {
        pre: function preLink(scope, iElement, iAttrs, controller) { ... },
        post: function postLink(scope, iElement, iAttrs, controller) { ... }
      }
    }
    
  2. From within compile returning only postLink implicitly:

    compile: function compile(tElement, tAttrs, transclude) {
      return function postLink( ... ) { ... }
    }
    
  3. From within link specifying both preLink and postLink explicitly:

    link: {
      pre: function preLink(scope, iElement, iAttrs, controller) { ... },
      post: function postLink(scope, iElement, iAttrs, controller) { ... }
    }
    
  4. From withing link using postLink implicitly:

    link: function postLink( ... ) { ... }
    
like image 58
Beyers Avatar answered Nov 15 '22 19:11

Beyers