我必须处理一个非 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/