Created
February 11, 2019 15:00
-
-
Save hollygood/75aefc77070cdc8e7b60289bb7d286cf to your computer and use it in GitHub Desktop.
Angular Material Slide / Switch button example
This file contains hidden or 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
// example for slide button, which value is not boolean | |
// example.html | |
<div class="form-group m-form__group"> | |
<mat-slide-toggle | |
formControlName="status" | |
[color]="primary" | |
(change)="changeStatus($event)" | |
> | |
{{ toggleStatusLabel }} | |
</mat-slide-toggle> | |
</div> | |
// example.ts | |
public toggleStatus: boolean = true; | |
public toggleStatusLabel: string; | |
ngOnInit() { | |
// existingData contains toggle status, which is 'ACTIVE' or 'BLOCKED' | |
const toggleStatus = this.existingData.status === 'ACTIVE' ? true : false; | |
this.toggleStatusLabel = this.existingData.status; | |
this.exampleForm.controls.status.setValue(toggleStatus); | |
} | |
changeStatus(e) { | |
this.toggleStatusLabel = e.checked === true ? 'ACTIVE' : 'BLOCKED'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment