remove item from stored array in angular 2

前端 未结 11 1894
长发绾君心
长发绾君心 2020-12-23 02:34

I want to remove an item from a stored array in angular 2, with Type Script. I am using a service called Data Service, the DataService Code:

export cl         


        
11条回答
  •  粉色の甜心
    2020-12-23 03:24

    You can't use delete to remove an item from an array. This is only used to remove a property from an object.

    You should use splice to remove an element from an array:

    deleteMsg(msg:string) {
        const index: number = this.data.indexOf(msg);
        if (index !== -1) {
            this.data.splice(index, 1);
        }        
    }
    

提交回复
热议问题