简体   繁体   English

如何从我的文本输入区域中删除“Fruit”值

[英]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.

相关问题 多个复选框输入到文本区域-添加文本-从文本区域删除文本 - Multiple Check box input to text area - add text - remove text from text area 如何删除文本字段/ text_area字段的值 - How to remove the value for text field/text_area field 我如何为window.getSelection值分配文本方向RTL,我试过但是如何从文本区域的现有值中删除getSelection - how to i assign text direction RTL for window.getSelection value, I tried but how to remove getSelection from existing value of text area 如何防止我的文本区域(专门输入类型的电子邮件)不被填充 - How do I prevent my text area (excually input type email) from not being filled 当我将div放在可放置区域时,如何获取输入文本值(从数据库)? - How to get the input text value (from database) when I drop the div on droppable area? 如何在文本框中的文本区域添加值? - How to add value in a text area from a textbox? 从输入值字符串中查找和删除文本 - Find and remove text from input value string 如何从文本区域获取显示为原始输入的文本 - How to get text to display like orginal input from text area 如何将文本从输入框传输到文本区域? - How Can I Transfer Text From Input Box to Text Area? 如何从输入文本中删除所选文本? - How to remove selected text from an input Text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM