我正在使用 electron-json-storage
并尝试将 AngularJS $scope
变量设置为等于存储的数据。
下面是我的代码:
portal.controller('portalViewController', function($scope) {
//$scope.url = "http://www.google.com"; //correctly sets $scope.url to google.com
storage.get('portal_url', (err, data) => {
console.log(data.url); //prints out data as it should
$scope.url = data.url;
});
});
如您所见,我拥有此工作的每个单独组件,但是当我将它们组合起来时,$scope.url
似乎并未设置。我已经被困在这个问题上有一段时间了,无法弄清楚问题是什么。我对 AngularJS 还很陌生,所以这可能是我所缺少的非常简单的东西。
感谢您提供的任何帮助!
最佳答案
由于storage
似乎是第三方插件,因此$scope
中的任何更改都不会执行摘要循环。使用 $timeout
强制摘要循环,以便在 UI 中呈现更改。
portal.controller('portalViewController', function($scope, $timeout) {
storage.get('portal_url', (err, data) => {
console.log(data.url); //prints out data as it should
$timeout(function(){
$scope.url = data.url;
});
});
});
关于javascript - AngularJS 作用域变量设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327053/