I have a "service" in setting part of my app. I push data in Firebase. In another component i have form with "md-select" that read the same data. The problem is that I can not initialize the list. Coming out and going back into the component everything works correctly. The list populates as expected.
this is my service in setting part.
import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Carburante } from './carburante.model';
@Injectable()
export class CarburantiService {
private basePath: string = '/carburanti';
carburanti: FirebaseListObservable<Carburante[]> = null; // list of objects
carburante: FirebaseObjectObservable<Carburante> = null; // single object
userId: string;//_____
constructor(private db: AngularFireDatabase,
private afAuth: AngularFireAuth) {
this.afAuth.authState.subscribe(user => {
if (user) this.userId = user.uid
})
this.carburanti = db.list(`/carburanti`);
}
getCarburantiList(query = {}): FirebaseListObservable<Carburante[]> {
if (!this.userId) return;
this.carburanti = this.db.list(`carburanti/${this.userId}`, { query: query }
);
return this.carburanti
}
...some code....
and the component:
import { Component, OnInit} from '@angular/core';
import { ReactiveFormsModule, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Carico } from '../carico.model';
import { CarichiService } from '../carichi.service';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
...some code...
import { Carburante } from '../../impostazioni/carburanti/carburante.model';
import { CarburantiService } from '../../impostazioni/carburanti/carburanti.service';
@Component({
selector: 'app-new-carico-form',
templateUrl: './new-carico-form.component.html',
styleUrls: ['./new-carico-form.component.css'],
})
export class NewCaricoFormComponent implements OnInit{
carburanti: FirebaseListObservable<Carburante[]>;
carico: Carico = new Carico();
caricoForm: FormGroup;
constructor(private fb: FormBuilder,
private carichiService: CarichiService,
private db: AngularFireDatabase,
private carburantiService: CarburantiService,
) {
this.createForm();
}
ngOnInit() {
this.carburanti = this.carburantiService.getCarburantiList() ;
}
createForm() {
this.caricoForm = this.fb.group({
dataCarico: ['', Validators.required],
riferimentoCarico: [''],
dettaglio: this.fb.group({
carburante: ['' ,Validators.required],
quantita: ['',Validators.required]
})
});
}
...some code...
why if I call the list in my ngOnit part, doesn't work? thanks