简体   繁体   English

Angular ng-repeat不更新数组

[英]Angular ng-repeat not updating array

I have an array as follows: 我有一个数组如下:

[7500, 15000, 21300, 3600]

This array is printed in the view using ng-repeat . 使用ng-repeat在视图中打印此数组。 But my problem is, I'm updating this array every second using setInterval , it is fired, and works on controller but updated value is not shown in view. 但是我的问题是,我使用setInterval每秒更新一次此数组,它已被触发并在控制器上运行,但是更新后的值未在视图中显示。 My code is given below: 我的代码如下:

JavaScript 的JavaScript

 angular
    .module('controllers')
    .controller('DashCtrl', DashCtrl);

 function DashCtrl($scope, $window) {

     var dash = this;
     dash.alarmList = JSON.parse() ; // [7500, 15000, 21300, 3600]

     dash.updateClock = function() {
        for(var key in dash.alarmList) {
            dash.alarmList[key] --;
        }
     }

     setInterval( dash.updateClock, 1000);
 }

HTML 的HTML

 <div class="list">

  <a class="item item-icon-left" ng-repeat="alarm in dash.alarmList">
    <i class="icon ion-person-stalker"></i>
    {{alarm}}
    <span class="badge badge-assertive">0</span>
  </a>

</div>

You should use $interval instead of setInterval which will internally call $scope.apply() to update bindings. 您应该使用$interval而不是setInterval ,它会在内部调用$scope.apply()来更新绑定。

 angular
    .module('controllers')
    .controller('DashCtrl', DashCtrl);

 function DashCtrl($scope, $window, $interval) {

     var dash = this;
     dash.alarmList = JSON.parse() ; // [7500, 15000, 21300, 3600]

     dash.updateClock = function() {
        for(var key in dash.alarmList) {
            dash.alarmList[key] --;
        }
     }

     $interval(dash.updateClock, 1000);
 }

Note: Intervals created by this service must be explicitly destroyed when you are finished with them. 注意:完成此服务后,必须显式销毁此服务创建的间隔。

Angular's wrapper for window.setInterval. Angular的window.setInterval的包装器。 The fn function is executed every delay milliseconds. fn函数每延迟毫秒执行一次。

The return value of registering an interval function is a promise. 注册间隔函数的返回值是一个承诺。 This promise will be notified upon each tick of the interval, and will be resolved after count iterations, or run indefinitely if count is not defined. 该承诺将在间隔的每个刻度上通知,并在计数迭代后解决,或者如果未定义计数则无限期运行。 The value of the notification will be the number of iterations that have run. 通知的值将是已运行的迭代数。 To cancel an interval, call $interval.cancel(promise). 要取消间隔,请调用$ interval.cancel(promise)。

In tests you can use $interval.flush(millis) to move forward by millis milliseconds and trigger any functions scheduled to run in that time. 在测试中,您可以使用$ interval.flush(millis)向前移动毫秒,并触发计划在该时间运行的任何功能。

Note: Intervals created by this service must be explicitly destroyed when you are finished with them. 注意:完成此服务后,必须显式销毁此服务创建的间隔。 In particular they are not automatically destroyed when a controller's scope or a directive's element are destroyed. 特别是在销毁控制器的作用域或指令的元素时,它们不会自动销毁。 You should take this into consideration and make sure to always cancel the interval at the appropriate moment. 您应该考虑到这一点,并确保始终在适当的时候取消间隔。 See the example below for more details on how and when to do this. 有关如何以及何时执行此操作的更多详细信息,请参见下面的示例。

You have to use $interval 您必须使用$ interval

$interval(function(){dash.updateClock}, 1000);

In your case, Angular doesn't get notified that the value has been changed and the digest cycle doesn't run. 在您的情况下,不会通知Angular值已更改且摘要周期未运行。 Some angular directives/services ( ng-click, $timeout, $interval, etc. ) automatically trigger the digest cycle. 一些角度指令/服务( ng-click, $timeout, $interval, etc. )会自动触发摘要周期。 So, either use $interval instead of setInterval or manually trigger the digest cycle by wrapping your for loop inside $scope.$apply(() => { /* your code here */ }); 因此,要么使用$ interval代替setInterval,要么通过将for循环包装在$scope.$apply(() => { /* your code here */ });来手动触发摘要循环$scope.$apply(() => { /* your code here */ });

There appears to be some logical flaws in your code. 您的代码中似乎存在一些逻辑缺陷。 Try the following: 请尝试以下操作:

<script>function DashCtrl() {

     var dash = this;
     var alarmlist = {
        0: 7500,
        1: 1500,
        2: 21300,
        3: 3600
      };

      dash.alarmList = alarmlist;


     dash.updateClock = function() {
        for(var i in dash.alarmList) {
            dash.alarmList[i] -= 1;
        }
        console.log(dash.alarmList);
     }

     setInterval( dash.updateClock, 1000);
 }
 DashCtrl();
 </script>

I assume you need the $interval wrapper too in the case of angular. 我假设您在有角度的情况下也需要$ interval包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM