简体   繁体   English

array.splice在Angular Js指令中显示奇怪的行为

[英]array.splice shows strange behavior in Angular Js directive

Checkout this plunkr 签出这个矮人
http://plnkr.co/edit/FQ7m6HPGRrJ80bYpH8JB?p=preview http://plnkr.co/edit/FQ7m6HPGRrJ80bYpH8JB?p=preview

In that After adding element when I delete element from array using splice method. 在那之后,当我使用拼接方法从数组中删除元素时。
Everything gets messed up. 一切都搞砸了。

code

Array.splice needs an array index. Array.splice需要一个数组索引。 The problem with your code is that you are passing the ngModel.index to the removeNode function and splicing an array with that index which is not actual array index. 代码的问题在于,您正在将ngModel.index传递给removeNode函数,并使用该index (不是实际的数组索引)对数组进行拼接。

You need to find the correct element index by looping through it and by checking its index property of each element. 您需要通过循环遍历并检查每个元素的index属性来找到正确的元素索引。 As I'm doing angular.forEach which seconds parameter gives us actual index of that array. 当我在做angular.forEach ,其中的seconds参数为我们提供了该数组的实际索引。

Code

    $scope.removeNode = function(index) {
      var foundIndex;
      angular.forEach($scope.sitemap, function(value, idx){
        if(value.index === index)
        foundIndex = idx;
      })

      $scope.sitemap.splice(foundIndex, 1);

    }

Demo Plunkr Demo Plunkr

When you create an object you hardcode the index of each new added object with you value of "abc" 创建对象时,请使用“ abc”值对每个新添加的对象的索引进行硬编码。

var abc = 0;
$scope.addNode = function(){
    abc++;
    var addObj = {name:"name"+abc, index:abc};
    $scope.sitemap.push(angular.copy(addObj));
}

Then when you removing and calling function with this hardcoded value of index 然后,当您使用索引的此硬编码值删除并调用函数时

$scope.removeNode = function(index){
    $scope.sitemap.splice(index,1);                     
}

So for example let say you have this array: 例如,假设您有以下数组:

obj1 - hardcoded index 0 - array index 0
obj2 - hardcoded index 1 - array index 1

if remove obj1 from array with index 0 your obj2 will have index 0 in the array then, 如果从索引为0的数组中删除obj1,则obj2在数组中的索引为0,

obj2 - hardcoded index 1 - array index 0

but in your case you still passing to the removeNode function index 1 as it was created with that value. 但是在您的情况下,您仍将传递给removeNode函数索引1,因为它是使用该值创建的。

I suggest another solution for that: Demo 我为此建议另一种解决方案: 演示

In my opinion: 在我看来:

  • sitemap should not define in current directive( sample ), because, it is list item(not list). sitemap不应在当前指令( sample )中定义,因为它是列表项(而非列表)。

  • Also, there is addNode method in your controller. 另外,您的控制器中有addNode方法。 there, removeNode should be declared. 在那里,应该声明removeNode

You can access controller's method in directive through removeAction: '&removeAction', : 您可以通过removeAction: '&removeAction',在指令中访问控制器的方法:

scope: {
    ngModel: "=ngModel",//or "="
    removeAction: '&removeAction',//or "&"
  },
  controller: ["$scope", function($scope) {
    $scope.removeNode = function(item) {
      $scope.removeAction({
        item: item
      });
    }
  }],

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

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