0

我在设置我的应用程序的一部分时有一个“服务”。我在 Firebase 中推送数据。在另一个组件中,我有一个带有“md-select”的表单,可以读取相同的数据。问题是我无法初始化列表。出来并回到组件中一切正常。该列表按预期填充。

这是我在设置部分的服务。

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....

和组件:

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...

为什么如果我在我的 ngOnit 部分中调用列表,它不起作用?谢谢

4

0 回答 0