Angularjs - ng-repeat不反映属性数组的变化

时间:2016-01-05 11:34:34

标签: javascript angularjs html5

我的HTML有一个带有ng-repeat的循环:

<div class="row">
    <div ng-repeat="item in model.painel track by item.codigoIncidente">
            <strong>{{item.restante.horaMinutoSegundo}}</strong>
    </div>
</div>

我的控制器有一个$ interval,它会减少horaMinutoSegundo。

nihilApp.controller('IncidentePainelController', ['$scope', '$interval', 'Incidente', function ($scope, $interval, Incidente) {

    $scope.model = {
        painel: [],
    };

    var find = function() {
        Incidente.listPainel({pagina: 0}).$promise.then(function (result) {
            $scope.model.painel = result.pagedList;
            $scope.model.totalItems = result.totalGeral;
        }, function (error) {
            console.log(error);
        });

    };

    var updateTime = function() {

        for (var i = 0; i < $scope.model.painel.length; i++) {
        if ($scope.model.painel[i].status.id === 'E' && $scope.model.painel[i].restante.segundos > 0) {
            var segundos = $scope.model.painel[i].restante.segundos - 1;
            $scope.model.painel[i].restante.horaMinutoSegundo = getHoraMinutoSegundo(segundos);
        }    
        }


    };


    var getHoraMinutoSegundo = function (segundos) {
        var horaMath = Math.floor(segundos / 3600);
        var remMath = Math.floor(segundos % 3600);
        var minutoMath = Math.floor(remMath / 60);
        var segundoMath = Math.floor(remMath % 60);

        var hrStr = (horaMath < 10 ? "0" : "") + horaMath;
        var mnStr = (minutoMath < 10 ? "0" : "") + minutoMath;
        var secStr = (segundoMath < 10 ? "0" : "") + segundoMath;

        return hrStr.concat(":").concat(mnStr).concat(":").concat(secStr);
    };    

    $interval(updateTime, 1000);

    find();

}]);

但是{{item.restante.horaMinutoSegundo}}不会在HTML中更新。有人可以帮我解决这个问题吗?非常感谢!

https://jsfiddle.net/araraujo/96w3jrrh/

4 个答案:

答案 0 :(得分:0)

  

您需要更改两件事

  1. 在控制器中注入$ interval

    app.controller(&#39; MainCtrl&#39;,function($ scope,$ interval){ }

  2. 更新您的模型,使其与for循环中的逻辑匹配。像下面的东西。

    $ scope.model = { painel:[{restante:1}]}

答案 1 :(得分:0)

你必须在最后添加$ scope.apply。

public class MusicStoreEntities : DbContext
{
    public DbSet<User> Users{ get; set; }

    public DbSet<Product> Products{ get; set; }
}

答案 2 :(得分:0)

它应该可以工作,但你的代码中有一点错误。

var segundos = $scope.model.painel[i].restante.segundos - 1;

您将新的segundos值放入变量中,但未从范围更新该值。

Here是更新版本的小提琴

答案 3 :(得分:0)

  

找到差距,替换此行

var segundos = $scope.model.painel[i].restante.segundos - 1;
  

使用

scope.model.painel[i].restante.segundos = $scope.model.painel[i].restante.segundos - 1;