[英]$watch is not working when model is updated in scope.$apply in directive
我有一個指令,我在指令范圍的'existingfiles'模型上添加了一個監視。 當模型通過范圍發生變化時。$ apply,監視器中沒有調用監聽器。
這是指令代碼,請告訴我,如果我在這里遺漏了一些東西,
directive('fileuploadPlugin', function() {
var linkFn;
linkFn = function(scope, element, attrs) {
angular.element(element).ready(function() {
jQuery('#fileupload').fileupload({
done: function (e, data) {
scope.$apply(function(scope) {
for(var i=0; i < data.result.filesuploaded.length; i++){
scope.existingfiles.push(data.result.filesuploaded[i]);
};
});
}
});
scope.$watch('existingfiles', function(newValue, oldValue) {
element.imagesLoaded(function() {
scope.$apply( function() {
element.masonry({
itemSelector : '.box'
});
});
});
});
};
return {
templateUrl : 'templates/fileupload_plugin.htm',
restrict : 'A',
scope : {
existingfiles : '=ngModel',
},
link : linkFn
};
})
這是我對指令的呼吁,
<div fileupload-plugin ng-model="existingfiles"></div>
請告訴我如何確保正確觀看模型
通過將'true'作為$ watch調用的第三個參數來解決問題。 請在這里找到關於第三個參數的更多信息,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.