Javascript 范围变量更新,但不更新视图
我有下面的函数,它构建了一个田径项目和课程的列表。我必须显示事件/类组合是否为“正式”,这是通过从后端收集正式组合列表并设置值Javascript 范围变量更新,但不更新视图,javascript,angularjs,Javascript,Angularjs,我有下面的函数,它构建了一个田径项目和课程的列表。我必须显示事件/类组合是否为“正式”,这是通过从后端收集正式组合列表并设置值event.isOfficial=true来完成的,前提是返回的组合与要显示的组合匹配 competitionsService.checkIsOfficial(selectedclasses, selectedevents) .then(function (data) { //Here we are building the class / event lis
event.isOfficial=true
来完成的,前提是返回的组合与要显示的组合匹配
competitionsService.checkIsOfficial(selectedclasses, selectedevents)
.then(function (data) {
//Here we are building the class / event list and checking if the event is official or not
$scope.currentEditItem.classes.forEach(function (cls) {
cls.events = selectedEvents;
cls.events.forEach(function (eventtype) {
eventtype.forEach(function (event) {
event.isOfficial = false; // By default no class / events combinations are official
data.forEach(function (item) {
if (item.idClass == cls.id && item.idEvent == event.id) {
event.isOfficial = true;
}
});
if ($scope.currentEditItem.classevent[cls.id][event.id].selected) {
$scope.addTag(event, cls.eventtags[event.intType],
$scope.currentEditItem.events[event.intType], event.intType, "classevents_" + cls.id + "_", false);
// Update the event tags
$scope.currentEditItem.classevent[cls.id][event.id].selected = !$scope.currentEditItem.classevent[cls.id][event.id].selected;
}
});
});
$scope.currentEditItem.classesbysex[cls.intSex].push(cls);
});
});
相关的html如下所示:
<ul class="inline">
<li ng-repeat="event in cls.events[eventtype.intType]" class="titlecell">
<div ng-click="setChecked(cls, event, cls.eventtags[eventtype.intType],currentEditItem.events[eventtype.intType],eventtype,'classevents_'+cls.id+'_')"
class="checkbox-div" name='event{{event.id}}'
id='classevents_{{cls.id}}_{{event.id}}'>
<i ng-class="{'icon-ok': currentEditItem.classevent[cls.id][event.id].selected}" class="iconpos"></i>
</div>
<div class="titlecell" ng-class="{warn:!event.isOfficial}">{{ event.idEvent }}: {{ event.strName }} - {{ event.isOfficial }} </div>
</li>
</ul>
-
{{event.idEvent}}:{{event.strName}}-{event.isoficial}
问题是,即使event.isOfficial在作用域中设置为true(当然,这已在调试器中检查过),它也不会在视图中更新
注:
在CompetitionService之前加载页面。调用checkIsoficial
。这将转到后端,并在承诺返回后更新DOM。但是,此回调中的其余数据将正确显示
$scope.$apply()
抛出一个错误,$digest已在进行中
在event.isOfficial=true
上方的三行中,我设置了event.isOfficial=false
。这反映在视图中。测试{{event.isoficial}
始终显示false
今天我做了几个小时后,头发几乎没有了。任何关于我可能做错了什么的想法都将不胜感激
谢谢
更新:
花了一些时间来获取正确的数据,但我有一个问题:
我能说的最好的问题是,您在相同事件上重复更改了Isoficial
标志,而不是在独立副本上执行操作。这可以通过在正确的位置应用angular.copy()
轻松解决
作为额外的奖励,我删除了一个嵌套循环,用一个简单的映射替换它
在plunker中,$scope
对象上似乎缺少一个函数,因此我将其注释掉
competitionsService.checkIsOfficial(selectedclasses, selectedevents)
.then(function (data) {
/// prevent a nested forEach-loop by using a map.
var map = {};
data.forEach(function (item) {
map[ [item.idClass,item.idEvent] ] = true;
});
//Here we are building the class / event list and checking if the event is official or not
$scope.currentEditItem.classes.forEach(function (cls) {
/// make a deep copy so the event-objects are independant.
cls.events = angular.copy(selectedEvents);
cls.events.forEach(function (eventtype) {
eventtype.forEach(function (event) {
event.isOfficial = map[ [cls.id, event.id] ] !== undefined;
if ($scope.currentEditItem.classevent[cls.id][event.id].selected) {
/// !! $scope.addTag does not exist in the plunker example.
/// $scope.addTag(event, cls.eventtags[event.intType], $scope.currentEditItem.events[event.intType], event.intType, "classevents_" + cls.id + "_", false);
// Update the event tags
$scope.currentEditItem.classevent[cls.id][event.id].selected = !$scope.currentEditItem.classevent[cls.id][event.id].selected;
}
});
});
$scope.currentEditItem.classesbysex[cls.intSex].push(cls);
});
});
这看起来比复制问题所需的最少代码多得多。也许一个$timeout
ed$scope.$apply()
会有帮助吗?这已经成功了一半,我不能把这个问题复制到其他地方!在循环的末尾添加$timeout ed$scope.$apply()没有任何区别。我真的无法在其中添加一个!对于$digest已在进行中的错误,请尝试在中包装$scope.$apply()
,如果(!$scope.$$phase)$scope.$apply()代码>。这基本上检查angular是否已经处于摘要阶段。但是,它不是一个正式的方法,只是一个解决办法。不确定它是否与问题有关,但是第一行中的<代码>选择符< /代码>与第五行中的<代码> >选择符> /代码>不匹配,因为在中间行中有一个省略的E。变量名称的选择很差,但是是的,应该是这样的。谢谢!太棒了,它工作得很好。同时也感谢map奖金——我将回顾代码的其他部分,在那里我可以使用相同的想法。