AngularJs 1.2.2:在ngRepeats中,通过使用$interval,ngShow会更新,但view不会更新
这是一个ngRepeat,它为用户重复发出警报。我想在一个时间段内逐个显示每个警报。我正在使用ngShow和一个计数器变量$scope.showedItem 模板:AngularJs 1.2.2:在ngRepeats中,通过使用$interval,ngShow会更新,但view不会更新,angularjs,angularjs-ng-repeat,intervals,ng-repeat,ng-show,Angularjs,Angularjs Ng Repeat,Intervals,Ng Repeat,Ng Show,这是一个ngRepeat,它为用户重复发出警报。我想在一个时间段内逐个显示每个警报。我正在使用ngShow和一个计数器变量$scope.showedItem 模板: <div class="container" > <div ng-repeat="headerNotification in headerNotification.list" ng-show="{{showedItem==$index}}">
<div class="container" >
<div ng-repeat="headerNotification in headerNotification.list" ng-show="{{showedItem==$index}}">
<h3>{{headerNotification.getText()}}</h3>
<button ng-click="headerNotification.do()" class="btn btn-warning">{{headerNotification.getButtonText()}}</button>
</div>
</div>
componentHeaderModule.controller('headerController', [
'$rootScope',
'$scope',
'componentHeaderNotification',
'$interval',
function ($rootScope, $scope, componentHeaderNotification, $interval)
{
...
$scope.showedItem = 1;
$interval(function(){
$scope.showedItem++;
if ($scope.showedItem > $scope.headerNotification.list.length)
$scope.showedItem = 0;
}, 5000);
}]);
当我查看DeveloperTool时,我看到ng show值每5秒更改一次,但是,视图没有刷新,始终显示相同的警报。甚至,items类也没有改变
有什么问题吗?我对双向绑定的理解是一个很大的错误?
ngShow
计算角度表达式,因此您不希望在其中使用双大括号
因此,内部:
<div ng-repeat="headerNotification in headerNotification.list" ng-show="{{showedItem==$index}}">
为此:
ng-show="showedItem==$index"
默认情况下,Angular不会在属性上放置$watch
(这对性能有很大帮助)。这就是Angular希望您传入表达式以进行计算的原因
当您在表达式中使用双大括号时,所发生的情况是,在链接指令时,仅对大括号中的表达式求值一次,然后将结果(在您的情况下,true
或false
)传入并永久使用,永远不会更改-即使属性的值发生更改,Angular从未注意到(因为上面没有$watch
)
例如,如果链接完成时showedItem
等于0,则传递第一个ngRepeat
条目true
,同时传递其他每个条目false
-导致所看到的内容,ngShow
永不改变
您希望表达式每$digest循环计算一次,以便传入表达式 真不敢相信!即使ngShow值正在更改,也不会产生任何影响!我需要更努力地学习!一个关键是angular不会在属性上放置手表。因此,在链接时传入的属性就是要计算的属性。现在您正在传入
true
或false
。即使html正在更改,也不会重新计算。这就是为什么要在链接中传递表达式。
ng-show="showedItem==$index"