简体   繁体   English

如何从已反转的列表中拼接/删除项目

[英]How to splice/remove an item from a list that was reversed

so im displaying an array in reverse order所以我以相反的顺序显示一个数组

*ngFor="let item of items.reverse(); let i = index"

The problem I'm having is that when I try to delete an item like so,我遇到的问题是,当我尝试删除这样的项目时,

deleteItem(row) {
    this.items.splice(row,1);
  }

EXAMPLE... array_items= [item1,item2,item3,item4];示例... array_items= [item1,item2,item3,item4]; So when its displayed, it will display array_items[3] first but if I try to delete it, it will delete array_items[0] .所以当它显示时,它会首先显示 array_items[3] 但如果我尝试删除它,它会删除 array_items[0] 。 Does that make sense?那有意义吗? it doesn't delete the correct one.它不会删除正确的。 How can I do this?我怎样才能做到这一点? Thanks in advance for the help.在此先感谢您的帮助。

Since the list is reversed, the index will not be row but this.items.length - row , so I would try:由于列表是颠倒的,索引不会是row而是this.items.length - row ,所以我会尝试:

deleteItem(row) {
    this.items.splice(this.items.length - (row + 1), 1);
}

Hi when you reverse items in ngfor , the value doesn't change just in view and the items value in your component class reversed .嗨,当您反转 ngfor 中的项目时,该值不会仅在视图中更改,并且您的组件类中的项目值已反转。 I think you should use costume reverse pipe :我认为你应该使用服装反向管道:

    import { Pipe, PipeTransform } from '@angular/core';

    @Pipe({ name: 'reverse' })

    export class ReversePipe implements PipeTransform {
        transform(value) {
            return value.slice().reverse();
        }
    }

so the items reversed just in view.所以这些项目只是在视图中颠倒了。

*ngFor="let item of items | reverse let i = item.length + index - 1;"

You should keep start as the length of the array subtracting the currentIndex plus 1.您应该将开始作为数组的长度减去 currentIndex 加 1。

deleteItem(row){
  this.item.splice(this.item.length - (row +1), 1);
}

This will remove the original item from the array.这将从数组中删除原始项目。

I suggess :我建议:

deleteItem(row){
this.item.splice(this.item.length - (row +1), 1);
}

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

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