简体   繁体   English

* ngFor的递减指数

[英]Decrement Index of *ngFor in Angular

Using Angular 4.0.2, I have an *ngFor creating input elements and updating an object array through an add button, but I'm trying to figure out how to update the objects' indeces once an item is removed from that array. 使用Angular 4.0.2,我有一个* ng用于创建输入元素并通过添加按钮更新对象数组,但是我试图弄清楚一旦从该数组中删除一个项目,如何更新对象的索引。

Currently, my ngFor looks like: 目前,我的ngFor看起来像:

*ngFor = let name of names; let i = index

Then, my delete button has a (click) event: 然后,我的删除按钮具有一个(单击)事件:

(click)="removeRow(i, names)"

And the method looks like: 该方法如下所示:

    removeRow(index:number, names:Names[]){
        names.splice(index,1);
        index--;
    }

But, when I go to update another input element in the array after removing an object, the indeces are incorrect. 但是,当我在删除对象后去更新数组中的另一个输入元素时,索引是不正确的。 It appears that "let i = index" in the *ngFor does not decrement, and I apparently cannot just use: 看来* ngFor中的“ let i = index”不会递减,而且我显然不能只使用:

(click)="removeRow(i, names); i--"

You do not need to manually modify the index set by *ngFor. 您不需要手动修改* ngFor设置的索引。 Angular takes care of it with its two-way data binding. Angular通过其双向数据绑定来处理它。 When you modify your "names" array in your component, the view will update itself to reflect the changes, this includes the index on your *ngFor. 当您在组件中修改“名称”数组时,视图将自动更新以反映更改,其中包括* ngFor上的索引。

*ngFor: * ngFor:

*ngFor="let name of names; let i = index"

Click event: 点击事件:

(click)="removeRow(i)"

Component method: 组成方法:

removeRow(index:number){
    this.names.splice(index,1);
}

Notice that you do not need to pass your array from your view to your component since you already have it in the component. 请注意,您不需要将数组从视图传递到组件,因为您已经在组件中拥有该数组。

Edit: Plunker 编辑: 柱塞

Without seeing more of your code ... this is only a guess. 没有看到更多的代码...这只是一个猜测。 But I assume you also have a names property of the component class? 但是我假设您还具有组件类的names属性?

Try not passing the names array into the method and instead just removing the element from the bound names property. 尝试不要将names数组传递给方法,而只是从绑定的names属性中删除该元素。 Angular`s data binding should then take care of the rest. 然后,Angular的数据绑定将处理其余的工作。

Template: 模板:

<tr *ngFor='let product of products; let i = index'>
    <td>
        <img    [src]='product.imageUrl'
                [title]='product.productName | uppercase'
                (click)="removeRow(i)">
    </td>
</tr>

I had an image so used the click on that ... but it will work the same on a button. 我有一个图像,因此单击该图像即可...但是在按钮上它的作用相同。

Component class: 组件类:

removeRow(index: number): void {
    this.products.splice(index,1);
}

Also, I displayed the value of "i" in the template using {{i}} and after deleting a row the "i" value is correctly adjusted for the new set of indices. 另外,我显示的“我”在模板中使用{{我}}和删除行“我”的价值正确调整为新的索引集后的值。

Isn't i being passed by value into your remove row function? i不是通过值传递给您的删除行函数吗? If so you'd have to decrement it in the same scope it exists (Within the ngFor loop). 如果是这样,则必须在存在的范围内递减它(在ngFor循环内)。

Example: https://snook.ca/archives/javascript/javascript_pass 示例: https//snook.ca/archives/javascript/javascript_pass

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

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