第一次推送到数组后,Angular不会绑定转发器 [英] Angular doesn't bind repeater after first push to array

查看:71
本文介绍了第一次推送到数组后,Angular不会绑定转发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到数组的角度中继器.当我推送新项目时,UI会按预期进行更新,但是对于后续推送,数组将进行更新,但UI不会更改.我已经尝试过 scope.$ apply(),但是没有任何区别.

I have an angular repeater bound to an array. When I push a new item, the UI updates as expected, but for subsequent pushes, the array updates but the UI doesn't change. I already tried scope.$apply() but it doesn't make any difference.

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
    $scope.maxValue = 6;
    $scope.values = [1,2,3,4,5,6];

    $scope.update = function (){
        $scope.maxValue++
        $scope.values.push($scope.maxValue);
        //$scope.$apply();
    }
});

不确定我在做什么错.这是一个复制问题的小例子.

Not sure what I'm doing wrong. Here's a small example that replicates the problem.

推荐答案

这是因为您一次又一次地重复使用相同的值会创建重复的元素,请尝试使用 按索引跟踪

It's because you are pussing the same value again and again which creates duplicate elements, try using track by index

 <li data-ng-repeat="item in values track by $index"> 

工作小提琴

这篇关于第一次推送到数组后,Angular不会绑定转发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆