设置来自服务的md-select输入

时间:2017-08-09 17:45:52

标签: angular firebase service angularfire ngoninit

我有一个"服务"在设置我的应用程序的一部分。我在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部分调用该列表,那么无效? 感谢

0 个答案:

没有答案