Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: disable switch programatically

moulot priority asked 4 years ago


how do i disable a switch programatically. with a variable for instance that can change value in the .ts something like disabled="true"


Konrad Stępień staff answered 4 years ago


Hi @moulot,

You can use [disabled]="variable" property in your code.

Like this:

<!-- Material switch -->
<div class="switch">
  <label>
    Off
    <input type="checkbox" (change)="toggle($event)">
    <span class="lever"></span> On
  </label>
</div>

<!-- Material switch -->
<div class="switch">
  <label>
    Off
    <input type="checkbox" [disabled]="disable">
    <span class="lever"></span> On
  </label>
</div>

And ts:

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  disable = false;

  toggle = (event: any) => {
    this.disable = event.target.checked;
  }
}

Best, Konrad.



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB Angular
  • MDB Version: 9.0.0
  • Device: dell notebook
  • Browser: chrome
  • OS: windows 10
  • Provided sample code: No
  • Provided link: No