内部指令首先执行 - 应该是第二个

时间:2014-12-23 09:24:30

标签: angularjs angularjs-directive

我有自定义指令,其中包含其他自定义指令。 问题是首先从内部指令执行link func而不是外部指令。为什么? 在外部指令中,我得到了对内部指令至关重要的计算。 优先权没有帮助。

以下是代码示例:

App.directive('ifSearchSelect', ['$system',function ($system) {
    return {
        restrict: "E",
        replace: true,
        scope: {
            ngModel: "=",
            kolumna: "="
        },
        require:"ngModel",
        priority:100,
        link: function (scope, element, attrs, ctrl) {
            console.log('Not first but I expected it to be');
            //more funcs
        },
        templateUrl: "template/if-form-pola/select.html"
    };
}])
.run(["$templateCache", function ($templateCache) {
    $templateCache.put("template/if-form-pola/select.html",
        "<div>" +
        "<if-input-select ng-model=\"model.ngModel.wartosc\" kolumna=\"::model.kolumna\" dane=\"::model.wartosci\"></if-input-select>" +
        "</div>"
    );
}])

.directive('ifInputSelect', ['$system',function ($system) {
    return {
        restrict: "E",
        replace: true,
        scope: {
            ngModel: "=",
            kolumna: "="
        },
        require:"ngModel",
        priority:50,
        link: function (scope, element, attrs, ctrl) {
            console.log('First supposed to be second');
            //more funcs
        },
        templateUrl: "template/if-input-pola/select.html"
    };
}])
.run(["$templateCache", function ($templateCache) {
    $templateCache.put("template/if-input-pola/select.html",
        "<div>" +
        "Input directive" +
        "</div>"
    );
}])

模板:<if-search-select ng-model="data" kolumna="kolumna"></if-search-select>

1 个答案:

答案 0 :(得分:2)

我在pre函数中找到了解决方案link - 这里的来源http://www.undefinednull.com/2014/07/07/practical-guide-to-prelink-postlink-and-controller-methods-of-angular-directives/

更改了第一个指令

App.directive('ifSearchSelect', ['$system',function ($system) {
    return {
        restrict: "E",
        replace: true,
        scope: {
            ngModel: "=",
            kolumna: "="
        },
        require:"ngModel",
        link: {
            pre: function (scope, element, attrs, ctrl) {
                console.log("Now it's first");
                //more funcs
            },
        }
        templateUrl: "template/if-form-pola/select.html"
    };
}])