简体   繁体   English

无法更新ng-repeat($ scope。$ apply())中的数组更新的视图不起作用

[英]Unable to update view on array update in ng-repeat ($scope.$apply() ) does not work

The view does not update if the array is updated inside a function. 如果在函数内部更新数组,则视图不会更新。 Although, if I console.log the array after array.push then it shows the updated array. 虽然,如果我在array.push之后用console.log记录数组,那么它将显示更新后的数组。 I have tried $scope.apply() inside $timeout still it does not work. 我在$ timeout内尝试了$ scope.apply()仍然无法正常工作。

JavaScript : JavaScript

$scope.openQuestions = [];

$scope.openQuestions.push({value: '1', question: 'abc1'});

$timeout(function() {        

  $scope.$apply(function() {            
    $scope.fetchOpen();      
  });

}, 500);

console.log($scope.openQuestions); //shows the updated value

$scope.fetchOpen = function() {
  $scope.openQuestions.push({value: '2', question: 'abc2'});
}

Html HTML

<ul>
 <li ng-repeat = "question in openQuestions">
 <p>{{question.question}} </p>
 </li>
</ul>

Result: abc1 结果:abc1

The problem is with the use of the Array.prototype.push function: 问题在于使用Array.prototype.push函数:

 $scope.openQuestions = $scope.openQuestions.push({value: '2', question: 'abc2'});

As said in the docs: 如文档中所述:

The push() method adds one or more elements to the end of an array and returns the new length of the array. push()方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。

It returns the length of the array and you put it in the reference variable that hold the array and effectively overrun it. 它返回数组的长度,并将其放入保存该数组并有效地覆盖它的引用变量中。

Instead, just use the push without setting the array again: 相反,只需使用推入而无需再次设置数组:

$scope.fetchOpen = function() {
  $scope.openQuestions.push({value: '2', question: 'abc2'});
}

@Sumit Rana Is the code which you posted exactly how you are using it, or did you strip it down for posting it here? @Sumit Rana您所发布的代码是否完全符合您的使用方式,或者是否将其剥离以在此处发布? The reason why I'm asking is, that I had nearly exactly the same issue, except that I was not using a timer like you. 我问的原因是,除了没有使用像您这样的计时器外,我遇到了几乎完全相同的问题。 Instead I had a simple function which should just add an element to an array. 取而代之的是,我有一个简单的函数,应该只向数组添加一个元素。 I was playing around with all those fiddles out there which worked fine. 我正在与所有那些提琴奏效的玩耍。 Unfortunately in my case ng-repeat did not update. 不幸的是,在我看来, ng-repeat没有更新。 Then it turned out that the reason was correlated to my (fairly complex) ng-repeat statement: 然后发现原因与我的(相当复杂的) ng-repeat语句有关:

<tr ng-repeat="cmr in cmrs | orderBy:crit:rev:sortByCrit | filter:{selected:search_selected, ip:search_ip, name:search_name, location:search_location}:false as filtered" class="row-eq-height">

The problem was that I was reading all attributes from a file, except the "selected" attribute. 问题是我正在从文件中读取所有属性,除了“ selected”属性。 This attribute I'm initializing after loading the data. 加载数据后,我正在初始化此属性。 Unfortunately I forgot to do this initialization as well after changing the array. 不幸的是,在更改数组后,我也忘记进行此初始化。 Thus this attribute was not existing in the scope and I always got an empty list. 因此,该属性在范围中不存在,我总是得到一个空列表。

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

相关问题 将值推送到数组后,Ng-repeat不会更新,尝试$ scope。$ apply()但返回错误 - Ng-repeat doesn't update after pushing value to array, tried $scope.$apply() but returns error 如何让AngularJS更新ng-repeat阵列接头上的DOM? $ scope。$ apply()返回错误 - How to get AngularJS to update the DOM on ng-repeat array splice? $scope.$apply() returns an error angular.js $ scope。$ apply不适用于ng-repeat - angular.js $scope.$apply wont work on ng-repeat 更改$ scope变量后,Angularjs ng-repeat不会更新 - Angularjs ng-repeat does not update after changing $scope variable 为什么需要$ scope。$来更新此数组 - Why is $scope.$apply necessary to update this array 角度ng-在ng-repeat中单击以更新$ scope,然后使用$ apply更新dom - angular ng-click inside ng-repeat to update $scope and then use $apply to update dom 搜索后角度ng-repeat视图不会更新 - Angular ng-repeat view does not update after a search 角度:删除元素后,ng-repeat不会在视图上更新 - Angular: ng-repeat does not update on view after removing the element 推送到数组不会通过ng-repeat指令更新视图列表 - Pushing to an array does not update view list via ng-repeat directive 角$ scope在更新,导航和$ scope。$ apply()之后未绑定到视图 - Angular $scope not binding to view after update, navigation and $scope.$apply()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM