[英]How to remove “Fruit” value from my text input area
So i need to remove the value that i added at the text input area as a chip, but i dont know how to solve the codes to link the it together and remove it. 所以我需要删除我在文本输入区添加的值作为芯片,但我不知道如何解决代码将它链接在一起并删除它。 So what should happen is when the proper codes are linked together, it should be able to be removed when i click the "x" on the chip. 那么应该发生的是当正确的代码链接在一起时,当我点击芯片上的“x”时,它应该能够被移除。
i'm able to add the values, but when i click remove the values are still there. 我能够添加值,但是当我点击删除时,值仍然存在。
add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
console.log(`mat chip`, event);
console.log(`mat chip value`, value);
// Add our fruit
if ((value || '').trim()) {
this.fruits.push({name: value.trim()});
console.log(`fruits`, this.fruits);
let type = this.editDeliveryOrderForm.value.type;
type += ',' + value.trim();
this.editDeliveryOrderForm.patchValue({
type
});
}
// Reset the input value
if (input) {
input.value = '';
}
}
remove(fruit: Fruit): void {
const index = this.fruits.indexOf(fruit);
const value = fruit.name;
// console.log(`mat chip`, event);
// console.log(`mat chip value`, value);
if (index >= 0) {
this.fruits.splice(index, 1);
// this.fruits.push({name: value.trim()});
// console.log(`fruits`, this.fruits);
let type = this.editDeliveryOrderForm.value.type;
value.trim();
this.editDeliveryOrderForm.patchValue({
type
});
}
Array method indexOf() is case sensitive. 数组方法indexOf()区分大小写。 The object which passed to fruits array does not return the index. 传递给fruits数组的对象不返回索引。
const index = this.fruits.indexOf(fruit);
//console.log(index);
if(index !== -1) {
// code..
}
You can use Array.filter() to filter the elements in array 您可以使用Array.filter()过滤数组中的元素
remove(fruit: Fruit): void {
this.fruits = this.fruits.filter((fr)=>fr.name !== fruit.name);
}
This will return new array removing the current fruit. 这将返回删除当前水果的新数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.