It seems to be so easy, but I just cannot get the checked binding of the slide toggle to work.
(I'm using angular 9.1 and material 9.2.)
Here's the html:
<mat-slide-toggle [checked]="isSlideChecked" (change)="toggleChanges($event)">isSlideChecked: {{isSlideChecked}}</mat-slide-toggle>
<p *ngFor="let e of toggleEvents">{{e}}</p>
Here's the TS:
import { Component, OnInit } from '@angular/core';
import { MatSlideToggleChange } from '@angular/material/slide-toggle';
@Component({
selector: 'app-test-component',
templateUrl: './test-component.component.html',
styleUrls: ['./test-component.component.scss']
})
export class TestComponentComponent implements OnInit {
public isSlideChecked: boolean = false;
public toggleEvents: string[] = [];
constructor() { }
ngOnInit(): void {
}
toggleChanges($event: MatSlideToggleChange) {
this.toggleEvents.push("Toggle Event: " + $event.checked)
}
}
The change event is working fine, $event.checked is always set correctly. But the checked binding just won't work, it's always staying false.
Before I give up and use an odinary checkbox, I hoped someone here could help me. Any suggestions?