Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind <mat slide toggle> using [(ngModel)] with a string value in HTML

I have a material slide toggle button and it is two way bind with a string variable having a value "true" or "false" using [(ngModel)], the button updates the value of variable correctly when i toggle it, but for the first time when it is loaded in DOM, it always shows its state to true even if the value in the variable is "false".

<div *ngIf="agent.attributes[i].type == 'Boolean'">
  <mat-slide-toggle [checked]="agent.attributes[i].value == 'true' ? true : false" 
                  [(ngModel)]="agent.attributes[i].value">{{agent.attributes[i].value}}</mat-slide-toggle>
                </div>

this is the result

like image 860
Raza Ellahi Avatar asked Dec 17 '18 09:12

Raza Ellahi


People also ask

How to use slide toggle in Angular Material?

This page will walk through Angular Material slide toggle example. To work with slide toggle we need to import MatSlideToggleModule in application module. Slide toggle is created using MatSlideToggle Directive. The selector of MatSlideToggle is mat-slide-toggle.

What is <mat-slide-toggle> in angular?

The <mat-slide-toggle>, an Angular Directive, is used as a on/off switch with material design styling and animation capabilities. In this chapter, we will showcase the configuration required to draw a slide toggle control using Angular Material.

Why does [ (ngmodel) ] bind to the value property of a string?

Since your value property is a string with a value of 'false' or 'true', the [ (ngModel)] binding will evaluate that to true in both cases. Ideally your value property would be a boolean.

How to create a slide toggle using matslidetoggle directive?

Slide toggle is created using MatSlideToggle Directive. The selector of MatSlideToggle is mat-slide-toggle. To create a slide toggle we use <mat-slide-toggle> element. MatSlideToggle provides input properties such as ariaLabel, ariaLabelledby, checked, color, disableRipple, disabled, id, labelPosition, name, required.


3 Answers

you are binding string value to ngModel that needs to be boolean for check, so change it to:

<div>
  <mat-slide-toggle 
      [checked]="agent.attributes[i].value === 'true' ? true : false"
      (change)="setValue( i , $event )">
      {{agent.attributes[i].value}}
  </mat-slide-toggle>
</div>

ts code:

setValue(i , e){
    if(e.checked){
        this.agent.attributes[i].value = 'true'
   }else{
        this.agent.attributes[i].value = 'false'
   }
}

DEMO

like image 159
Fateme Fazli Avatar answered Oct 19 '22 04:10

Fateme Fazli


The issue with your code is that your [(ngModel)] binding is overwriting the [checked] binding. Remove the [(ngModel)] binding and you can see that the [checked] binding works just fine.

Since your value property is a string with a value of 'false' or 'true', the [(ngModel)] binding will evaluate that to true in both cases.

Ideally your value property would be a boolean. Why does it need to be a string?

With the property as a boolean you could even get rid of the [checked] binding like so:

<div *ngIf="agent.attributes[i].type == 'Boolean'">
  <mat-slide-toggle [(ngModel)]="agent.attributes[i].value">{{agent.attributes[i].value}}</mat-slide-toggle>
</div>

If it has to be of type string then you can use a getter/setter in your component like in this Stackblitz.

like image 24
Fabian Küng Avatar answered Oct 19 '22 05:10

Fabian Küng


I tried here with an example a little bit different, but I hope it will help you.
According to the docs:
https://material.angular.io/components/slide-toggle/api
This component has a Output propertie named change wich:

An event will be dispatched each time the slide-toggle changes its value.

You can try to add:

(change)="agent.attributes[i].value= !agent.attributes[i].value"

As you can see here:
https://stackblitz.com/edit/angular-b77drk?file=app/slide-toggle-configurable-example.html

like image 2
Guilherme Bordallo Avatar answered Oct 19 '22 05:10

Guilherme Bordallo