Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kaidanov/3376f61ad56448a4a4a1108db002185c to your computer and use it in GitHub Desktop.
Save Kaidanov/3376f61ad56448a4a4a1108db002185c to your computer and use it in GitHub Desktop.
Angular 5 Reactive Forms - Checkbox array mat-selection-list simple sample
<div class="col-md-6" *ngIf="notifications && notifications.length > 0">
<label class="example-margin">Notifications to send:</label>
<mat-selection-list [(ngModel)]="selectedNotifications" [ngModelOptions]="{standalone: true}">
<mat-list-option *ngFor="let n of notifications" [checkboxPosition]="before" [value]="n.id+'_'+n.description">
<mat-icon matListIcon>notifications</mat-icon>
{{n.description}}
</mat-list-option>
</mat-selection-list>
</div>
...
this.yourService.getnotifications().subscribe(response => {
this.notifications = ...;
});
...
createForm() {
this.form = this.fb.group({
...
notification: this.fb.array([])
});
}
...
addContact() {
this.updateSelected(this.selectedNotifications,'notification');
...
}
updateSelected(selected, key) {
let chkArray = <FormArray>this.form.get(key);
chkArray = this.fb.array([]);
selected.forEach(element => {
let item = element.split('_');
chkArray.push(new FormControl({ id: item[0], description: item[1] }));
});
}
revert() {
this.form.reset();
this.selectedContactTypes = null;
this.selectedNotifications = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment