Analysis of controller and link function in angularJS directive

Source: Internet
Author: User

In Angularjs, you have a series of view that is responsible for rendering the data to the user; You have a controller that manages the $scope (view model) and exposes the relevant behavior (defined by $scope) to the view You have some directive to link the user interaction and $scope behavious. But there is one thing: a directive controller. This directive controller child is defined in a directive context, But it can also be injected to other directives as a way to facilitate inter-directive communication.

ANGULARJS directive should be the most important and powerful, in DDO, there are two places to introduce custom functions, one is the Controller property, one is the link property, what is the difference between these two properties of Directive? Should our code be put in the directive controller or the link attribute?

Ask yourself: What time do I want this code to run? The answer to this question determines where you should put the code:

    • Run before compilation? -Put it into the controller attribute.
    • Want to expose an API to other directives? -Go to the controller attribute to define the API
    • Run after compilation? -Put it in the link attribute

Angular the time order in which the Compile,link,controller is compiled:

Angular.module (' compilation ', []). Directive (' Logcompile ',function($rootScope) {$rootScope. Log= ""; return{controller:function($scope, $attrs) {$rootScope. Log= $rootScope. Log + ($attrs. Logcompile + ' (Controller) \ n '); }, compile:functioncompile (element, attributes) {$rootScope. Log= $rootScope. Log + (Attributes.logcompile + ' (compile) \ n '); return{pre:functionPreLink (scope, element, attributes) {$rootScope. Log= $rootScope. Log + (Attributes.logcompile + ' (pre-link) \ n '); }, Post:functionPostlink (scope, element, attributes) {element.prepend (attributes.logcompile); $rootScope. Log= $rootScope. Log + (Attributes.logcompile + ' (post-link) \ n ');    }      }; }  };}). Directive (' Terminate ',function() {  return{terminal:true  };});

http://jasonmore.net/angular-js-directives-difference-controller-link/

Analysis of controller and link function in angularJS directive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.