简体   繁体   English

Angular 2 从数组中添加或删除项目

[英]Angular 2 Add or Remove item from Array

I have this method here:我这里有这个方法:

messageSelected($event, index) {
  console.log($event.target.checked);
  console.log(index);
  if ($event.target.checked) {
    this.selectedMessages.push($event.target.getAttribute('name'));
  }
  else {
    var item = this.selectedMessages.indexOf($event.target.getAttribute('name'), 0);
    this.selectedMessages.slice(item, 1);
  }
  console.log(this.selectedMessages);
}

What I am trying to do is if an item is checked, add the item to the array (this part works) now I am trying to remove the item from the array if the checked is false (this part is not working)我想要做的是,如果一个项目被选中,则将该项目添加到数组中(这部分工作)现在我试图从数组中删除该项目,如果检查为假(这部分不起作用)

The problem is that the item is not getting removed from the array.问题是该项目没有从数组中删除。 What am I doing wrong?我究竟做错了什么?

Here is my HTML:这是我的 HTML:

<div class="card" *ngFor="let i of userMessages; let item = index">
  <div class="card-body row-eq-height clearfix">
    <div class="col-sm-2 vertical-center text-center">
      <div class="avatar">
      </div>
    </div>
    <div class="col-sm-8 vertical-center xs-center-text">
      <strong class="username">Username</strong>
      <p><a class="read-more" [routerLink]="['/singleMessage/' + i.id]">{{i.subject}}</a></p>
    </div>
    <div class="col-sm-2  col-xs-6  clearfix">
      <span class="date">{{i.updated_at | date: 'MM/dd/yyyy'}}</span>
    </div>
    <div class="col-sm-1  col-xs-6  text-right clearfix no-x-padding">
      <div class="custom-check pull-right">
        <input type="checkbox" id="{{i.id}}" name="{{i.id}}" (change)="messageSelected($event, item)"/>
        <label for="{{i.id}}"></label>
      </div>
    </div>
  </div>
</div>

AngularJS binds to the Array Object, and the slice function in JS creates a new array as a result, leaving the original array as it is. AngularJS 绑定到 Array 对象,JS 中的slice函数因此创建了一个新数组,原数组保持原样。

You should use splice (note the p) which will update the array.您应该使用splice (注意 p)来更新数组。

For example:例如:

var index = this.selectedMessages.indexOf($event.target.getAttribute('name'));
this.selectedMessages.splice(index, 1);

Besides the confusion between slice (not mutating option) and splice , it seems like you're using angular as vanilla js.除了slice (not mutating option) 和splice之间的混淆之外,似乎您正在使用 angular 作为vanilla js。

The whole idea of using a framework like this, is to get rid of DOM inspection / manipulation.使用这样的框架的整个想法是摆脱 DOM 检查/操作。

You don't need to overload the <input name> , you could call directly messageSelected($event.target.checked, i.id) (modifying the function firm as well, providing all the needed data).您不需要重载<input name> ,您可以直接调用messageSelected($event.target.checked, i.id) (同时修改函数公司,提供所有需要的数据)。

Better than that, you could use two way binding [(ngModel)]="selectedItems[i.id]" to bind the checkbox to a map (object) of "selected items", and then simply extract the true entries, mapping this to an array.比这更好,您可以使用两种方式绑定[(ngModel)]="selectedItems[i.id]"checkbox绑定到“所选项目”的映射(对象),然后简单地提取true条目,映射这个到一个数组。

Or, if you dont mind to mutate the i object, you could bind ngModel directly on a custom property like [(ngModel)]="i.selected" .或者,如果您不介意改变i对象,您可以将ngModel直接绑定到自定义属性上,例如[(ngModel)]="i.selected"

The idea is to avoid wiring manually the change and the getAttribute('name') .这个想法是为了避免手动连接changegetAttribute('name')

Here's a living example: https://codesandbox.io/s/rjxn1y84wm这是一个活生生的例子: https : //codesandbox.io/s/rjxn1y84wm

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

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