2

Background

I have a few switches from CoreUI in my app that toggle on and off. I need to be able to read the current state of these switches, "on" or "off". I also need to be able to set the state of them based on some other factors.

Question

I have not been able to find an example of this anywhere. How can I access the switch to obtain the current state and set the state to either on or off?

Code Example

   <label class="switch switch-text switch-pill switch-success switch-sm pull-right">
       <input type="checkbox" class="switch-input" checked>
       <span class="switch-label" data-on="On" data-off="Off"></span>
       <span class="switch-handle"></span>
    </label>

1 Answer 1

2
<input type="checkbox" [(ngModel)]="mySwitch">
<div> switch on: {{mySwitch}}</div>
class MyComponent {
  mySwitch: boolean = true;

}
Sign up to request clarification or add additional context in comments.

3 Comments

Hey man I think I misunderstood your answer. It made sense when I read it but how does this add the checked attribute based on the boolean? Notice inside the input at the end of it in the example checked. Checked value exist when it is switched on. Would you still handle this in the same way?
If mySwitch is true (as it is in my example) then ngModel will assign that to <input type="checkbox">. [(...)]="..." is two-way-binding.
Wow ya great thanks man. How do you always know everything? It never ceases to amaze me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.