简体   繁体   English

通过node.js函数更改变量时触发$ digest

[英]Trigger $digest when variable changed by node.js function

I want to make an application with node-webkit using AngularJS. 我想使用AngularJS通过node-webkit创建一个应用程序。 No problem so far but when I want Angular to watch over a "legacy variable" by using the method described in this answer I just get an Error: [$rootScope:inprog] . 到目前为止没有问题,但是当我希望Angular通过使用此答案中描述的方法监视“旧版变量”时,我只会得到一个Error: [$rootScope:inprog]

I wrapped my node.js function into $scope.$apply() like this: 我将node.js函数包装到$scope.$apply()如下所示:

$scope.$apply(myNodeModule("test", function(err, res) {
        if (err) {return console.log(err)}
        myLegacyVar = res;
}));

And watching over the (global) myLegacyVar variable like described in the answer mentioned before: 并监视(全局) myLegacyVar变量,如前面提到的答案中所述:

function () {
            return $window.myLegacyVar
        }, function(n,o){
            console.log("changed ",n);
            $scope.data = n;
        }
);

All of the code resides within my Angular controller function. 所有代码都驻留在我的Angular控制器函数中。

The function is async, so I guess it could have to do with that? 该函数是异步的,所以我想这可能与它有关吗? Or is this function inception I have going on in $apply maybe causing my error? 还是我在$ apply中进行的此函数初始化可能导致我的错误?

I'm just clueless right now, I have just recently started using AngularJS, hours of googleing haven't gotten my very far. 我现在一无所知,我最近才开始使用AngularJS,数小时的Google搜索还没到我的头。

This answer helped. 这个答案有所帮助。 The safe $apply function there did the trick. 安全的$ apply函数可以解决问题。 I added that function to my controller and added this snipped to my node.js function: 我将该函数添加到控制器中,并将其片段添加到我的node.js函数中:

var scope = angular.element($("#angularDOMElement")).scope();
scope.safeApply(function(){
    myLegacyVar = res;
})

I don't know if thats the proper way to do it but its working. 我不知道这是否是正确的方法,但是可以正常工作。

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

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