我正在开发一种角离子 - 火基应用程序。我正在努力解决一个我无法在我的html中更新$ scope的问题。我必须解决{{uevent}}并使用{{uevent}}的结果更新html。我的HTML代码如下。
**<ion-view view-title="{{uevent}}">**
<ion-tabs class="tabs-stable tabs-icon-top">
<ion-tab title="Buddies" icon="ion-ios-people" ng- click="showBuddies(a)">
</ion-tab>
<ion-tab title="Summary" icon="ion-navicon" ng-click="billSummary()">
</ion-tab>
</ion-tabs>
我的角度代码如下。 CurrUser是一家工厂。我已经定义了一个空的$ scope.uevent,我期望使用从工厂返回的值来更新。
var eventid = CurrUser.getEventid();
$scope.uevent = " ";
function updateEvent(desc) {
$scope.uevent = desc;
console.log($scope.uevent);// I am able to see the value returned from the factory. This function is executed below in the for loop.
};
// promise that returns a value from the factory - CurrUser.
CurrUser.getEventdesc(eventid).then(function (result) {
var description = result;
var desc;
for (var itemID in description) {
var tmp = description[itemID];
desc = tmp.Description; // This contains the value that must be updated to $scope.uevent
updateEvent(desc); // Calls the function defined above to update $scope.uevent.
};
});
自昨晚以来,我一直没有任何线索。非常感谢任何帮助,一如既往,感谢您的时间。
更新#1:(7月7日) 当我添加1秒的超时时,视图已更新为正确的值。
function updateEvent(desc) {
$timeout(function(){
$scope.$apply(function () {
$scope.uevent = desc;
})
},1000);
};
此致
答案 0 :(得分:1)
试试这个; $ apply用于触发$ digest。
function updateEvent(desc) {
$timeout(function(){
$scope.uevent = desc;
$scope.$apply();
},0)
};
答案 1 :(得分:1)
你的控制器在html中的定义在哪里?或者它在你的app.js.无论如何,一开始你可以在你的updateEvent函数中使用$ scope吗?$。
function updateEvent(desc) {
$scope.$apply(function(){
$scope.uevent = desc;
});
console.log($scope.uevent);
};
我不知道你的getEventDesc()方法会发生什么,但如果有非角度的ajax请求或其他东西,那么可能需要$ apply来触发摘要周期并更新观察者。
参考:这篇精彩的信息帖子 - &gt; https://github.com/angular/angular.js/wiki/When-to-use- $范围。$申请()
AngularJS为常见的本机JS异步行为提供包装器:
活动=&gt; NG-点击 超时=&gt; $超时 jQuery.ajax()=&gt; $ HTTP 这只是一个带有$ scope的传统异步函数。$ apply()在最后调用,告诉AngularJS刚刚发生异步事件。
$ scope。$ apply()应该尽可能接近async事件绑定。
请勿在整个代码中随意撒上。如果你在做 if(!$ scope。$$ phase)$ scope。$ apply()它因为你在调用堆栈中不够高。
尽可能使用AngularJS服务而不是本机服务。如果您正在创建一个AngularJS服务(例如套接字),它应该有一个$ scope。$ apply()它会触发一个回调。
答案 2 :(得分:1)
使用AngularJS时有2个执行上下文 - Angular Context
和JavaScript Context
。
当您更改AngularJS Context
中的数据时,digest loop
开始并且所有数据都会更新,但是当您更改JavaScript Context
中的数据时(例如setTimeout,setInterval等),Angular不会知道更改,因此它不会更新数据。
在JavaScript上下文中,您必须使用scope.$apply()
方法更改数据以手动运行digest loop
。
所以你的代码看起来像
var eventid = CurrUser.getEventid();
$scope.uevent = " ";
function updateEvent(desc) {
$scope.$apply(function(){
$scope.uevent = desc;
});
console.log($scope.uevent);// I am able to see the value returned from the factory. This function is executed below in the for loop.
};
// promise that returns a value from the factory - CurrUser.
CurrUser.getEventdesc(eventid).then(function (result) {
var description = result;
var desc;
for (var itemID in description) {
var tmp = description[itemID];
scope.$apply(function(){
desc = tmp.Description; // This contains the value that must be updated to $scope.uevent
})
updateEvent(desc); // Calls the function defined above to update $scope.uevent.
};
});
更多。如果有Angular JS替代方案,则必须使用它,因为它会自动运行te摘要循环($timeout
而不是setTimeout()
,$interval
而不是{{1 }})