17

ディレクティブ スコープの「existingfiles」モデルにウォッチを追加したディレクティブがあります。scope.$apply を介してモデルに変更がある場合、watch でリスナーへの呼び出しはありません。

ここにディレクティブ コードがあります。ここで何か不足している場合はお知らせください。

   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>

モデルが適切に監視されていることを確認する方法を教えてください

4

1 に答える 1

40

$ watch呼び出しの3番目のパラメーターとして「true」を指定することにより、問題が解決されました。3番目のパラメータの詳細については、こちらをご覧ください。

$rootScopeのAngularDocs

于 2012-10-07T12:41:54.487 に答える