How to update ion-toggle status?


#1

Hi, I have an ion toggle in my application that stores his status in a firebase Database, is there a way to change his status with the data loaded from firebase?

For example, if in the database the status is true, then when I load the view the toggle will be on. I’ve tried using checked:

<ion-toggle checked="status()" >

where status() returns a boolean.

But due to the async function of firebase, the view loads 1st before the value in status(). Can’t find a solution to this problem so far, I’d apreciate any help.


#2

I’ve never used Firebase, but the way you’ve written this, the checked property is set to the string value “status()”, which is probably not what you want. You need to say [checked], but please keep reading.

It’s fairly rare that you want to really be binding [checked], because normally, you want the user’s interaction with the toggle to affect a bound property. In that case, checked is irrelevant, and you want [(ngModel)].

Also, it is a gigantic red flag when you are ever typing () in a template. Angular calls template expressions a zillion times, and it is virtually always a mistake to have them be a method call instead of a property access.


#3

Thanks! I solved it using [checked], I’m not used using ngModel yet.