I would like to safe a boolean in Firebase Database. So far no problem. I can change the status of this boolean out of my App but now comes the problem. I would like to check the status of this boolean and make it as the standard of my toggle and if the boolean changes his value from true to false or opposite. I would like to live update this in my app. Do you think that this is possible and do you know how to do that?
Here comes my code:
fire-base.ts:
import { Component, NgZone } from '@angular/core';
import { IonicPage, NavController, ActionSheetController, AlertController } from 'ionic-angular';
import { AngularFireDatabase, AngularFireList} from 'angularfire2/database';
import { Observable } from "rxjs";
@IonicPage()
@Component({
selector: 'page-fire-base',
templateUrl: 'fire-base.html',
})
export class FireBasePage {
relaisList:AngularFireList<any>;
Relais: Observable<any[]>;
public relais_1: boolean = true;
constructor(public navCtrl: NavController, public afDB: AngularFireDatabase, public alertCtrl: AlertController,
public actionSheetCtrl: ActionSheetController) {
this.relaisList = this.afDB.list('/Relais')
this.Relais = this.relaisList.valueChanges();
}
changeRelais_1() {
if (this.relais_1) {
this.relaisList.update('relais_1', {
status: true})
}else {
this.relaisList.update('relais_1', {
status: false})
}
}
ionViewDidLoad() {
}
}
I know how to make a ion-list but the problem is, that i need the single status: true in my .ts file and i can’t find a way to get this working. Thank you for every help!!
Hey @Alberthoekstra i tried it but there are so many parameters missing that i don’t know how to get it working
Hope it is clear what i want. 1. I would like to have access to just the true or false to work with it and the second think i’m looking for is an auto refresh on change for the toggle.
I’m really a newbie with ionic and firebase so hopefully someone could help me there
UPDATE with the change to npm install angularfire2@4.0.0-rc0 --save
It’s all about using Angular and firbase together with reatime updates.
The basic premise of what you want to do is make your switch monitor a firebase document as an observable using the valueChanges pipe. This will open a realtime comnection to the database and the boolean value will always reflect what’s in the db. Then to update it make toggling the switch change the value in the db. The reset will be automatic.