Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs-ng repeat不反映属性数组中的更改_Javascript_Angularjs_Html - Fatal编程技术网

Javascript Angularjs-ng repeat不反映属性数组中的更改

Javascript Angularjs-ng repeat不反映属性数组中的更改,javascript,angularjs,html,Javascript,Angularjs,Html,My HTML有一个循环,其中包含ng repeat: <div class="row"> <div ng-repeat="item in model.painel track by item.codigoIncidente"> <strong>{{item.restante.horaMinutoSegundo}}</strong> </div> </div> {{item.res

My HTML有一个循环,其中包含ng repeat:

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

{{item.restante.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();

}]);
nihilApp.controller('incidentPaineController',['$scope','$interval','incident',函数($scope,$interval,incident){
$scope.model={
佩内尔:[],
};
var find=function(){
listPainel({pagina:0})。$promise.then(函数(结果){
$scope.model.painel=result.pagedList;
$scope.model.totalItems=result.totalGeral;
},函数(错误){
console.log(错误);
});
};
var updateTime=function(){
对于(变量i=0;i<$scope.model.painel.length;i++){
if($scope.model.painel[i].status.id=='E'&&$scope.model.painel[i].restante.segudos>0){
var segudos=$scope.model.painel[i].restante.segudos-1;
$scope.model.painel[i].restante.horaMinutoSegundo=getHoraMinutoSegundo(segunds);
}    
}
};
var getHoraMinutoSegundo=函数(segundos){
var horaMath=数学楼层(segundos/3600);
var remMath=数学地板(segudos%3600);
var minutoMath=数学楼层(remMath/60);
var segundoMath=Math.floor(remMath%60);
var hrStr=(horaMath<10?:“)+horaMath;
变量mnStr=(分钟数<10?:“)+分钟数;
var secStr=(segudomath<10?:“)+segudomath;
返回hrStr.concat(“:”).concat(mnStr.concat(“:”).concat(secStr);
};    
$interval(updateTime,1000);
查找();
}]);
但是{item.restante.horaMinutoSegundo}}不会在HTML中更新。有人能帮我解决这个问题吗?非常感谢

有两件事你需要改变

  • 在控制器中注入$interval

    应用控制器('MainCtrl',函数($scope,$interval){ }

  • 更新您的模型,使其与for循环中的逻辑相匹配

    $scope.model={ 佩内尔:[{restante:1}]}


  • 必须在末尾添加$scope.apply

    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);
        }    
        }
    
    $scope.apply();
    };
    
    var updateTime=function(){
    对于(变量i=0;i<$scope.model.painel.length;i++){
    if($scope.model.painel[i].status.id=='E'&&$scope.model.painel[i].restante.segudos>0){
    var segudos=$scope.model.painel[i].restante.segudos-1;
    $scope.model.painel[i].restante.horaMinutoSegundo=getHoraMinutoSegundo(segunds);
    }    
    }
    $scope.apply();
    };
    
    它应该可以工作,但您的代码中有一点错误

    var segundos = $scope.model.painel[i].restante.segundos - 1;
    
    您将新的
    segudos
    值放入变量中,但没有从作用域更新该值

    是小提琴与更新版本

    发现间隙,更换此线


    你是否在你的控制器中注入了
    $interval
    ?控制台中有错误吗?是的。我注入了$interval,控制台中没有出现错误。你可以提供一个JSFIDLE/plunkr吗?@Kosmo,JSFIDLE是我不知道你的模型是什么样子的$scope.model.painel是从REST检索到的json数组。它已经有restante字段了。谢谢,伙计,找到下面的答案。
    var segundos = $scope.model.painel[i].restante.segundos - 1;
    
    scope.model.painel[i].restante.segundos = $scope.model.painel[i].restante.segundos - 1;