javascript - AngularJS 作用域变量设置不正确

标签 javascript angularjs electron

我正在使用 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/

相关文章:

javascript - 我如何从 ckeditor 中删除链接工具栏?

javascript - Angular Directive(指令)链接函数中的 "attrs"参数有什么用

javascript - 附加具有与类匹配的 ID 的元素

AngularJS 加载局部 View

angular - Electron/Angular 应用程序可以访问文件夹中的视频。

javascript - 我不能在 Electron 中使用fs.readFile函数

javascript - 如何使用 jQuery 删除 div 中的最后一个 BR 元素

angularjs 无法找到模板目录,flask 作为后端

angularjs - AngularJS 模板中的 Grails Assets 图像

javascript - Electron webview标签在执行JavaScript(CoffeeScript)后不执行回调