Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NativeScript/Angular - How to style checked state on a switch when using ngModel/FormBuilder?

Consider this switch:

<Switch class="switch" formControlName="answer"></Switch>

If I do this it only works when you haven't activated the switch yet, after that the background-color will always be the same even when the switch is not active:

.switch[checked=true] {
  background-color: #FE4A49;
  color: #FE4A49;
}

And if I do this:

.switch {
  background-color: #FE4A49;
  color: #FE4A49;
}

Then the background will always be the same regardless of the state.

What's the correct way of styling a switch when using it with angular's model bindings?

like image 891
Chrillewoodz Avatar asked Oct 15 '25 03:10

Chrillewoodz


2 Answers

i'm styling an App and this is i'd do.

CSS:

Switch#userStatus[checked=true]{
  color:#ff0048 ;
  background-color: white;
  transform: scale(1.25, 1.25); // This is for change the size when checked
  transform: translate(-5,0); // This is for stay the position after scale it
}

Switch#userStatus[checked=false]{
  color: gray;
  background-color: gray;
}

And this is my XML:

`<Switch id="userStatus" checked="false" />`

I hope this helps!

like image 68
Donny Vasquez Ramos Avatar answered Oct 17 '25 23:10

Donny Vasquez Ramos


I had an issue with switch

background-color

and it was not working as expected. Changing the background-color was changing the whole switch (the gray part). So I had to change only the color property:

Switch[checked=true] {
    color: #f68533;
}
like image 34
madsongr Avatar answered Oct 17 '25 23:10

madsongr