gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:39:57 24 4
gpt4 key购买 nike

我已经浏览过这个链接 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com