我放棄了試圖找到後鏈接掛鉤,而是試圖以不同的方式思考問題。
目標是讓指令通知一個對象,該對象用作查找各種模型屬性的「默認」值。因此,例如,像這樣:
<my-directive ng-model="foo" default-value="bar"></my-directive>
本來我的指令看起來是這樣的:
.directive("myDirective", function() {
return {
scope: { ngModel: '=', defaultValue: '@' },
link: function(scope, elem, attrs) {
$scope.ngModel = $scope.defaultValue;
// ...etc...
}
}
})
我意識到,這可以在過程中完成,只要編譯步爲默認值間沒有」 t綁定。我看中了該解決方案看起來是這樣的:
.service("defaultValues", function(someOtherDependency) {
this.aDefault = someOtherDependency.getDefault();
})
.directive("myDirective", function(defaultValues) {
return {
scope: { ngModel: '=' },
compile: function(elem, attrs) {
defaultValues[attrs.ngModel] = attrs.defaultValue;
}
// ...etc...
}
})
.controller("theController", function($scope, defaultValues) {
$scope.values = angular.copy(defaultValues);
})
這裏的區別是,我注射服務(單體)到我的指令,並在編譯時就可以設置一個值。這發生在控制器函數被調用之前,所以單例完全按照它的時間填充。
我希望這可以幫助別人!
http://stackoverflow.com/questions/20241360/angular-js-getting-the-element-from-inside-evalasync-in-directive – iamwhitebox
@ whiteb0x文檔說* $ evalAsync不保證何時表達式將被執行* – AlexFoxGill
嘗試了上述 - 仍然運行在其他指令 – AlexFoxGill