简体   繁体   English

当模型在范围内更新时,$ watch不起作用。$ apply in directive

[英]$watch is not working when model is updated in scope.$apply in directive

i have a directive where i have added a watch on the 'existingfiles' model of the directive scope. 我有一个指令,我在指令范围的'existingfiles'模型上添加了一个监视。 When there is change in the model through scope.$apply, there is no call to listener in watch. 当模型通过范围发生变化时。$ apply,监视器中没有调用监听器。

Here is the directive code, kindly let me know if i am missing something here, 这是指令代码,请告诉我,如果我在这里遗漏了一些东西,

   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
    };  
     })

Here is my call to directive, 这是我对指令的呼吁,

<div fileupload-plugin ng-model="existingfiles"></div>

Kindly let me know how to make sure that model is watched properly 请告诉我如何确保正确观看模型

Problem has been resolved by giving 'true' as third parameter of $watch call. 通过将'true'作为$ watch调用的第三个参数来解决问题。 Please find here, the more information on third parameter, 请在这里找到关于第三个参数的更多信息,

Angular Docs on $rootScope $ rootScope上的Angular Docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM