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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form [formGroup]="form" novalidate> | |
<mat-form-field class="full-width"> | |
<mat-chip-list #chipList> | |
<mat-chip *ngFor="let requirement of form.get('requirements').controls; let i = index;" [selectable]="selectable" | |
[removable]="removable" (removed)="remove(i)"> | |
{{requirement.value}} | |
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon> | |
</mat-chip> | |
<input placeholder="New Requirement..." | |
[matChipInputFor]="chipList" | |
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" | |
[matChipInputAddOnBlur]="addOnBlur" | |
(matChipInputTokenEnd)="add($event)" /> | |
</mat-chip-list> | |
</mat-form-field> | |
<pre>{{form.value | json}}</pre> | |
</form> |
The TS component file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnInit} from '@angular/core'; | |
import {FormGroup, FormBuilder, FormArray} from '@angular/forms'; | |
import {MatChipInputEvent} from '@angular/material'; | |
import {ENTER, COMMA} from '@angular/cdk/keycodes'; | |
@Component({ | |
selector: 'chip-test', | |
templateUrl: './chip.component.html' | |
}) | |
export class ChipComponent implements OnInit { | |
form: FormGroup; | |
//chips | |
visible: boolean = true; | |
selectable: boolean = true; | |
removable: boolean = true; | |
addOnBlur: boolean = true; | |
// Enter, comma | |
separatorKeysCodes = [ENTER, COMMA]; | |
constructor(private fb: FormBuilder) { | |
this.createForm(); | |
} | |
createForm(): void { | |
this.form = this.fb.group({ | |
requirements: this.fb.array([]) | |
}); | |
} | |
ngOnInit(): void { | |
} | |
add(event: MatChipInputEvent): void { | |
let input = event.input; | |
let value = event.value; | |
// Add our requirement | |
if ((value || '').trim()) { | |
const requirements = this.form.get('requirements') as FormArray; | |
requirements.push(this.fb.control(value.trim())); | |
} | |
// Reset the input value | |
if (input) { | |
input.value = ''; | |
} | |
} | |
remove(index: number): void { | |
const requirements = this.form.get('requirements') as FormArray; | |
if (index >= 0) { | |
requirements.removeAt(index); | |
} | |
} | |
} |
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
This comment has been removed by the author.
ReplyDeleteThanks a lot for this ! :)
ReplyDeleteThank you for sharing. This has been very useful. Saved hours of figuring out.
ReplyDeleteReally super.
ReplyDeleteThank you so much
Very helpful, Thank you so much !! Saved a lot of time.
ReplyDeleteBut it appears you forgot a "d" on your html template, the event called is (removed) not (remove)
Thanks for seeing that
DeleteGreat example. Can you show how to display errors using mat-error? For example, requiring at least one value.
ReplyDelete
Delete[...]
Error sample
its very use full. but i need using data in object how to work with matchips and autocomplete?
ReplyDeletecan u show how to add validations in chip input with formcontrolname
ReplyDeleteThanks and that i have a dandy offer: What Renovation Expenses Are Tax Deductible house renovation stardew
ReplyDelete