Last active
May 30, 2018 15:04
-
-
Save Kaidanov/3376f61ad56448a4a4a1108db002185c to your computer and use it in GitHub Desktop.
Angular 5 Reactive Forms - Checkbox array mat-selection-list simple sample
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
<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 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
... | |
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