我正在开发一个基于角度的离子火力的应用程序。我在无法更新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);
};
问候,
当您使用AngularJSAngular 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替代方案,则必须使用它,因为它会自动运行dig摘要循环($timeout
而不是setTimeout()
,$interval
而不是setInterval()
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句