简体   繁体   English

AngularJS DOM在将元素添加到$ scope时更新,但不删除

[英]AngularJS DOM updates on adding element to $scope but not for deleting

I have an Angular 1.x app and I am able to successfully update the DOM via ng-repeat when adding an element to an array in the controller. 我有一个Angular 1.x应用程序,当向控制器中的数组添加元素时,我能够通过ng-repeat成功更新DOM。 However the DOM does not update when I try to remove an element. 但是,当我尝试删除元素时,DOM不会更新。

Here is the relevant part of the view: 这是视图的相关部分:

<ul class="pagination">

    <span ng-click="page(left)">left arrow</span>

    <li ng-repeat="i in range track by $index">

        <a href="" id="current-page-{{ $index * 5 == beginning }}" ng-click="pageStep( $index )">{{ $index + 1 }}</a>

    </li>

    <span ng-click="page(right)">right arrow</span>

</ul>

In my controller I have this variable: 在我的控制器中,我有以下变量:

  $scope.range = [1,2,3,4,5,6,7,8,9,10];

and here is my actual page() function: 这是我实际的page()函数:

  $scope.page = function ( direction ) {
      var lastnumber = $scope.range[$scope.range.length - 1];
      $scope.range.push(lastnumber + 1);
      $scope.range.shift();
  }

If I remove the last line in the function, the shift(), then the DOM will update accordingly. 如果我删除函数的最后一行shift(),则DOM将相应更新。 However, when I add the shift() back in the the DOM will neither add the new element nor delete the old. 但是,当我在DOM中添加shift()时,既不会添加新元素也不会删除旧元素。

  • I have tried adding in a $scope.$apply to this function which yielded no results. 我尝试将$ scope。$ apply添加到此函数,但未产生任何结果。
  • I have also tried using splice(0,1) instead of shift to no results. 我也尝试过使用splice(0,1)而不是没有结果。
  • Lastly, I have tried removing the 'track by' in the ng-repeat. 最后,我尝试在ng-repeat中删除“ track by”。 Again this did not fix anything. 同样,这没有解决任何问题。

This function does however update the $scope.range object properly, when I print to console I see the object gets the new number added and the first number deleted. 但是,当我打印到控制台时,该函数确实正确更新了$ scope.range对象,我看到该对象添加了新编号,并删除了第一个编号。 It just does not seem to update in the browser. 它似乎在浏览器中似乎没有更新。

Any ideas? 有任何想法吗?

Make sure that you pass in your ngclick page function parameter either 'left' or 'right' as string, otherwise they will be pass as variables and be undefined in the controller. 确保您以字符串形式传入ngclick页面函数参数“ left”或“ right”,否则它们将作为变量传递并且在控制器中未定义。

Now the main issue with the code was that you were referencing the $index in your <a>{{$index + 1}}</a> tags, but you should be using the element itself <a>{{i}}</a> (without adding 1). 现在,代码的主要问题是您在<a>{{$index + 1}}</a>标记中引用了$ index,但是您应该使用元素本身<a>{{i}}</a> (不加1)。 So your code was working you were just not binding the correct variable to the view. 因此,您的代码在工作,您只是没有将正确的变量绑定到视图。

Using the $index as you are using it might also not give you the expected behavior you want for your id and ng-click in the <a></a> attributes. 在使用$ index的同时使用它可能也无法获得想要的id和ng单击<a></a>属性的预期行为。

 angular.module('app', []) .controller('myController', function($scope) { $scope.range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $scope.page = function(direction) { if (direction === 'right') { var lastnumber = $scope.range[$scope.range.length - 1]; $scope.range.push(lastnumber + 1); $scope.range.shift(); } else if (direction === 'left') { /// move left code } } }) 
 <div ng-app="app" ng-controller="myController"> <ul class="pagination"> <span ng-click="page('right')">&#62;</span> <li ng-repeat="i in range track by i"> <a href="" id="current-page-{{ $index * 5 == beginning }}" ng-click="pageStep( $index )">{{ i + 1 }}</a> </li> </ul> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> 

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

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