Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two way binding on angular material slide toggle not working as expected (Angular 4)

I have implemented the angular material slide-toggle that all seems to work except for some reason it isn't binding the value to the relevant variable for some reason?

// other irrevelant imports above..
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.scss'],
    host: {
     '(document:click)': 'handleClickEvent($event)',
  }
})

export class CalendarComponent implements OnInit {

  filteringSchedule: boolean = false;
  filteringSent: boolean = false;
  filteringFailed: boolean = false;
}

// component

<mat-slide-toggle
    class="calendar-filter-types"
    [ngModel]="(filteringSchedule)"
    [color]=""
    [checked]="">
    Scheduled : {{ filteringSchedule }}
</mat-slide-toggle>

Once I check or uncheck the toggle I would expect the filteringSchedule value to change to true or false accordingly? However within the component it always stays as false for some unknown reason - can anyone suggest why is this?

I am Angular 4

like image 826
Zabs Avatar asked Mar 07 '18 15:03

Zabs


3 Answers

This works on Angular8+

[(ngModel)]="filteringSchedule"

And make sure you already imported FormsModule

import {FormsModule} from '@angular/forms';
like image 136
Binh Vo Avatar answered Nov 01 '22 03:11

Binh Vo


Just update your html to

[(ngModel)]="filteringSchedule"
like image 44
HMarteau Avatar answered Nov 01 '22 04:11

HMarteau


[ngModel]="(filteringSchedule)"

Change this to:

[(ngModel)]="filteringSchedule"
like image 2
Karan Mishra Avatar answered Nov 01 '22 04:11

Karan Mishra