Skip to content

Instantly share code, notes, and snippets.

@hollygood
Created February 11, 2019 15:00
Show Gist options
  • Save hollygood/75aefc77070cdc8e7b60289bb7d286cf to your computer and use it in GitHub Desktop.
Save hollygood/75aefc77070cdc8e7b60289bb7d286cf to your computer and use it in GitHub Desktop.
Angular Material Slide / Switch button example
// 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