You're right, but you didn’t use the “Slide Component” , this seems like a weird default behavior, although it seems to work:
Taken from your plunker:
Template
<md-card> <md-slide-toggle ngDefaultControl (change)="onChange($event)" [checked]="device"></md-slide-toggle> {{device}} </md-card>
TS
import {Component} from '@angular/core'; @Component({ selector: 'slide-toggle-overview-example', templateUrl: './slide-toggle-overview-example.html', }) export class SlideToggleOverviewExample { device:number = 1; onChange(e: Event) { if (e.checked == true) { this.device = 1; } else { this.device = 0; } } }
Working plunker
At first, I thought you used the ngModel binding and click binding at the same time, but that was not the case (since I eventually noticed that you are using unidirectional). They seem to go out of sync right from the start when you try to assign a numerical value instead of boolean .
Since this works the same as expected:
Template
<md-card> <md-slide-toggle ngDefaultControl [(ngModel)]="device"></md-slide-toggle> {{device}} </md-card>
TS
import {Component} from '@angular/core'; @Component({ selector: 'slide-toggle-overview-example', templateUrl: './slide-toggle-overview-example.html', }) export class SlideToggleOverviewExample { device:boolean = true; }
Despite the fact that the ng team knows at least the version of this problem mentioned in the release, “ Slide Switching Event - (Change) will be, even if the slider has not changed. ”
Steve G.
source share