angularjs - 在 angularjs 指令中哪个函数将首先执行?链接或 Controller ?

标签 angularjs angularjs-directive

我已经浏览过这个链接 https://www.undefinednull.com/2014/07/07/practical-guide-to-prelink-postlink-and-controller-methods-of-angular-directives/ ,他们说链接和 Controller 的执行顺序(从头到尾)是

  • Controller ,
  • 预链接功能,
  • 后链接功能

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

    最佳答案

    如果是第一个 link然后 controller不可能 require其他指令,并在 link 中使用他们的 Controller 功能。
    看一下来自 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;
    });
    
    为了支持这一声明,我们可以阅读同一页:

    controller

    Controller constructor function. The controller is instantiated before the pre-linking phase and can be accessed by other directives (see require attribute). This allows the directives to communicate with each other and augment each other's behavior.

    关于angularjs - 在 angularjs 指令中哪个函数将首先执行?链接或 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41162043/

    相关文章:

    javascript - Function 不是 AngularJS 中的函数

    javascript - 如何从另一个指令切换隔离范围的变量

    javascript - Controller 和外部指令之间的两种方式数据绑定(bind)

    javascript - 指令仅被调用一次

    angularjs - 将 Markdown 集成到 AngularJS 中?

    javascript - Angular HTTP GET 中更改的 JSON 中的字符

    javascript - Angular 相同的模块的多个指令

    javascript - 使用javascript从css文件中获取元素的样式

    angularjs - 如何获取 ngRepeat 指令内输入字段的 NgModelController?

    javascript - 在 grails 项目中使用 jquery 2.2.2