Chip input using Reactive Form

The Chips component documentation from the angular material website, describes how to use the MatChipInput directive with a chip-list for adding or removing chips.

I'll show you how you can implement the same component using Reactive Form.

The HTML template:



The TS component file: 




As you can see, we use the FormBuilder class to simplify the form creation. We define our requirements as a FormArray within our FormGroup.

The requirements will be accessed in our template with this expression inside the *ngFor of <mat-chip></mat-chip>:

*ngFor="let requirement of form.get('requirements').controls; let i = index;"

In the ChipComponent class, the requirements array is accessed by using this expression:

this.form.get('requirements') as FormArray;

Thanks


Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks a lot for this ! :)

    ReplyDelete
  3. Thank you for sharing. This has been very useful. Saved hours of figuring out.

    ReplyDelete
  4. Really super.
    Thank you so much

    ReplyDelete
  5. Very helpful, Thank you so much !! Saved a lot of time.
    But it appears you forgot a "d" on your html template, the event called is (removed) not (remove)

    ReplyDelete
  6. Great example. Can you show how to display errors using mat-error? For example, requiring at least one value.

    ReplyDelete
  7. its very use full. but i need using data in object how to work with matchips and autocomplete?

    ReplyDelete
  8. can u show how to add validations in chip input with formcontrolname

    ReplyDelete
  9. Thanks and that i have a dandy offer: What Renovation Expenses Are Tax Deductible house renovation stardew

    ReplyDelete

Post a Comment

Popular posts from this blog

Spring JPA : Using Specification with Projection