触发调整大小事件导致错误$ digest已经在进行angularjs

时间:2017-07-25 14:47:45

标签: javascript angularjs

我试图在我的angualr js指令上设置resize active,因为我的dygraph始终以scope.option.eidth = 0开头,即使我设置了值...经过多次尝试后我发现了这个:

window.dispatchEvent(new Event('resize'));

在我的浏览器中它可以工作!但是,如果我把这不是我的指令,它会引发错误

angular.js:11655 Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.3.15/$rootScope/inprog?p0=%24digest

我知道这是由范围状态引起的,而不是应用..是否有办法验证这一点?

我的代码:

(function() {
    var app = angular.module('dashboard.view', []);

    app.directive('dygraph', function(){
        return {
            restrict: 'E',
            scope: {
                data    : '=',
                options : '=?'
            },
            template: '<div id="graphDiv" style="width: 100%"></div>',
            link: function (scope, elem, attrs) {
                var div = elem.children()[0];
                scope.options.width = elem.parent()[0];

                console.log("scope.$$phase ",scope.$$phase);
                var graph = new Dygraph(div, scope.data, scope.options);
                if(scope.options.title=="Scanning" || scope.options.title=="Bytes" || scope.options.title=="Pakets"){

                    graph.ready(function(){

                        if(scope.$$phase!='$diggest'){
                            window.dispatchEvent(new Event('resize'));
                        }
                    });

                    console.log("scope.options.width 2",scope.options.width);
                }

            }        
        };
    });

})();

0 个答案:

没有答案