嗯,這是奇怪的,由具有編譯,預鏈接和postlink有大量日誌的例子,並得到了奇怪的結果:
function MyCtrl($scope) {
$scope.message = 'hello';
console.log('$scope.message set to hello');
}
app.directive('exampleDirective1', function() {
return {
restrict: 'E',
compile: function(tElement, tAttrs) {
console.log('currently at compile, attrs.min = ', tAttrs.min);
return {
post: function postLink(scope, elm, attrs) {
scope.$watch(function(){return attrs.min}, function (newValue, oldValue) {
console.log('currently, attrs.min = ', attrs.min);
});
console.log('currently at postlink, attrs.min = ', attrs.min);
},
pre: function prelink(scope,elm , attrs) {
console.log('currently at prelink, attrs.min = ', attrs.min);
}
};
}
};
});
http://jsfiddle.net/qdzLhpf9/
預鏈接運行時,範圍值已經設置。但是,這些論點似乎是在後鏈接和預鏈接之間進行評估的。
編輯: 經過一些更多的挖掘,似乎像$編譯初始化args到'undefined'在你的角度版本。請參閱https://code.angularjs.org/1.0.0/angular-1.0.0.js在4434行。
您真的需要$ watch嗎?我的意思是,爲什麼不使用隔離範圍綁定? – BiAiB
我可能錯過了一些東西,但問題是什麼? – miensol
哦,我知道了,他想知道爲什麼'console.log(attrs.min);'返回'undefined' – BiAiB