简体   繁体   English

在Angular中使用双向数据绑定在范围之间传递数组的正确方法是什么?

[英]What is the right way of passing array between scopes using two-way data binding in Angular?

Assuming that there is an angular custom directive which uses an isolated scope for array object with two-way binding: 假设有一个角度自定义指令,它使用具有双向绑定的数组对象的隔离范围:

// scope values
this.scope = {
  myArray: '='
};

So, now if I will add objects to the array everything will work like a charm: 所以,现在如果我要向数组添加对象,一切都会像魅力一样:

myArray.push(item); // array in the parent scope will be updated

However, when I will try to remove object from array via slice method it will not work - array will remain to be the same: 但是,当我尝试通过切片方法从数组中删除对象时,它将无法工作 - 数组将保持不变:

myArray.slice(itemIndex, 1); // ERROR: array will remain to be the same - item will not be deleted

Basically, slice creates a new array object which, as I understand, is prohibited by angular. 基本上,slice会创建一个新的数组对象,据我所知,它是由角度禁止的。 So, the question is - what is the right way of passing array between scopes via two-way data binding with an ability of adding / removing objects ? 所以,问题是 - 通过双向数据绑定以及添加/删除对象的能力,在范围之间传递数组的正确方法是什么?

EDIT: many thanks to @ryanyuyu for the correct answer and his patience in explaining. 编辑:非常感谢@ryanyuyu的正确答案和耐心解释。 In a nutshell: 简而言之:

  • the problem has nothing in common with Angular and coupled with vanilla JavaScript API. 这个问题与Angular没有任何共同之处,还有vanilla JavaScript API。
  • read documentation meticulously because sometimes one character really matters ;-) 仔细阅读文档,因为有时候一个角色真的很重要;-)

Reading the documentation for Array.slice : 阅读Array.slice文档

slice does not alter. slice不会改变。 It returns a shallow copy of elements from the original array. 它返回原始数组中元素的浅表副本。

Also of note, the first argument is the starting index, and the second argument is the ending index. 另外值得注意的是,第一个参数是起始索引,第二个参数是结束索引。 To actually change your scope variable, you need to assign the return value like so: 要实际更改范围变量,您需要像这样分配返回值:

myArray = myArray.slice(itemIndex, itemIndex+1);

More likely, you are looking to directly manipulate your array. 更有可能的是,您希望直接操作阵列。 Use Array.splice (that's splice with a p ) to remove elements from the array. 使用Array.splice (用p拼接)从数组中删除元素。

myArray.splice(itemIndex, 1);

This is an alternative solution that takes advantage of one-way bindings. 这是一种利用单向绑定的替代解决方案。 Two-way bindings should be avoided when you can in Angular. 你可以在Angular中避免双向绑定。 They're more expensive than one-way. 他们比单程更贵。 I'm adding it because it establishes firm control of the variables involved, and it may be informative to those exploring this type of data binding. 我正在添加它,因为它建立了对所涉及变量的牢固控制,并且它可能对探索这种类型的数据绑定的人提供信息。 (requires at least Angular 1.5) (至少需要Angular 1.5)

.directive('myDirective', [function(){
  return {
    scope:{
      myArray: '<',
      onArrayUpdate: '&'
    },
    link: function(scope) {
      scope.doThings = function(){
        scope.onArrayUpdate({index_1: itemIndex, index_2: 1})
      }
    }
  }
}])

.controller('myController', ['scope',function(scope){
  scope.myArray =  [];
  scope.arraySlice = function(index_1, index_2) {
    scope.myArray = scope.myArray.slice(index_1, index_2);//or whatever your intent is
  }
}])

<my-directive my-array="myArray" on-array-slice="arraySlice(index_1, index_2)"></my-directive>

Try changing this: 尝试改变这个:

myArray.slice(itemIndex, 1);

to this: 对此:

myArray = myArray.slice(itemIndex, 1);

It should work. 它应该工作。

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

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