javascript - 处理非 Angular 代码时如何立即更新 $scope?

标签 javascript angularjs angularjs-scope

我必须处理一个非 Angular 库,并且需要在它们之间创建通信。

<div id="MoipWidget" data-token="{{personaltoken}}" callback-method-success="successCB" callback-method-error="errorCB"></div>

每次加载页面时,我都必须从服务器获取 token 。

$http.post('https://example.org', obj)
        .success(function(data){
            $scope.personaltoken = data.token;
            //Here I call the non angular library and I get and error telling me that the token is undefined.
            //If I run the code from inside a $timeout works as I need...

        })
        .error(function(data){
             alert('error');
        });

我也尝试在 $scope.$apply 内运行,但收到​​一条错误消息,提示 $digest 已在进行中

我必须调用的非 angularjs 库很简单,只有两行。

var settings = {} 
LibraryCall(settings);

如何立即更新模型?

最佳答案

我尝试按照 @Kjell 的建议使用 $scope.$evalAsync 但没有成功。

阅读有关 $scope 的更多信息后,我找到了我需要的内容。

$scope.$applyAsync(function(){
    var settings = {} 
    LibraryCall(settings);
});

$scope.$applyAsync 将安排稍后调用 $apply。

https://docs.angularjs.org/api/ng/type/ $rootScope.Scope

关于javascript - 处理非 Angular 代码时如何立即更新 $scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664573/

相关文章:

javascript - Angularjs:调用 iframe 中的其他范围

javascript - 在 $scope.$on 内调用 $scope.$digest 根本不正确吗?

javascript - 动态内容注入(inject)的 Webview 限制

javascript - 按相关性对 UI-GRID 记录排序

javascript - 如何删除数据表中本地存储中表中的特定行

javascript - 独立指令绑定(bind)到父函数

angularjs - Angular 和 Internet Explorer 11 - 输入无法正常工作

javascript - AngularJS 在单击 ng-click ng-repeat 时替换列表

动态创建的 html 表出现 Javascript 错误 "Null is not an Object"

javascript - AngularJS + d3js : Issues with resizing objects