2016-12-15 58 views
1

我已经通过这个环节https://www.undefinednull.com/2014/07/07/practical-guide-to-prelink-postlink-and-controller-methods-of-angular-directives/走了,他们说链路和控制器的执行顺序(第一个到最后)是哪个函数将首先在angularjs指令中执行?链接或控制器?

  1. 控制器,
  2. 预Link功能,
  3. 后链接功能

但是在这里我读了AngularJS: What is the need of the directive's link function when we already had directive's controller with scope?链接在控制器之前执行。我应该相信哪一个?

回答

1

如果它是第一个link然后controller它不可能require其他指令和使用他们的控制器在link函数。

看看代码从documentation

var directiveDefinitionObject = { 
    controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... }, 
    controllerAs: 'stringIdentifier', 
    require: 'siblingDirectiveName', // requiring another directive  
    compile: function compile(tElement, tAttrs, transclude) { 
     return { 
     pre: function preLink(scope, iElement, iAttrs, controller) { ... }, //siblingDirectiveName's controller is available in link function 
     post: function postLink(scope, iElement, iAttrs, controller) { ... } 
     }   
    }, 
    }; 
    return directiveDefinitionObject; 
}); 

为了支持这一说法,我们可以读取Ø在同一个页面:

控制器

控制器构造函数。控制器在之前实例化预链接阶段,并且可以通过其他指令访问(请参阅require属性)。这允许指令相互沟通并增强彼此的行为。

相关问题