SoberdogK9
SoberdogK9

Reputation: 127

How do I remove a text from a string in my coding

So I got this project I'm working on, I'm successful in adding the text(chips) to the string, but when I remove it, the text value is still there. So something is wrong in the remove part of my code. I need help removing the string and solving it!

My mentor says this is where I need to add changes to the code so I can remove the string from type and make it work. (in between these two codes)

let type = this.editDeliveryOrderForm.value.type;
// remove the string from type
this.editDeliveryOrderForm.patchValue({
  type
});

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);

  if (index >= 0) {
    this.fruits.splice(index, 1);

    let type = this.editDeliveryOrderForm.value.type;

    // remove the string from type

    this.editDeliveryOrderForm.patchValue({
      type
    });
  }

Upvotes: 0

Views: 123

Answers (1)

Mohamed Ali RACHID
Mohamed Ali RACHID

Reputation: 3297

Here is how you can assign an empty string to type form control :

remove(fruit: Fruit): void {
     const index = this.fruits.indexOf(fruit);

     if (index >= 0) {
        this.fruits.splice(index, 1);
        this.editDeliveryOrderForm.patchValue({
           type : ""
        });
     }
}

Upvotes: 1

Related Questions