我從異步服務的功能中檢索Firebase中的數據,並且希望在我的組件中檢索它們。從組件中的異步服務中檢索數據
我有我的數據庫中的數據,該函數很好地返回我的數據,但我不能在我的home.ts(數組爲空)中檢索它們。
todolistService.ts:
import {Injectable} from '@angular/core';
import {AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';
import * as moment from 'moment';
import 'rxjs/add/operator/map';
import {Observable} from "rxjs/Observable";
@Injectable()
export class TodolistService {
statuts: Array<any> = [];
dateOfTheDay: string;
constructor(public afDB: AngularFireDatabase) {
moment.locale('fr');
this.dateOfTheDay = moment().format('L'); // Date au format : 04/07/2017
}
/**
*
* @returns {Observable<Array<any>>}
*/
statusToShow():Observable<Array<any>> {
let localStatuts: Array<any> = [];
return this.afDB.list('/statut').map(status => {
for (let s of status) {
if (this.dateOfTheDay === s.statut_date_tache) {
if (s.statut_id_tache in this.statuts === false) {
localStatuts[s.statut_id_tache] = s;
console.log('=== STATUSTOSHOW ===');
console.log(localStatuts);
return localStatuts;
}
}
}
});
}
}
home.ts:
import {Component} from '@angular/core';
import {ModalController, NavController} from 'ionic-angular';
import {TodolistService} from "../../providers/todolistService";
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public statusOfTheDay: Array<any> = [];
constructor(public navCtrl: NavController, public modalCtrl: ModalController, public todolistService: TodolistService) {
}
ionViewDidLoad() {
this.todolistService.statusToShow().subscribe(status => this.statusOfTheDay = status);
console.log('=== HOME ===');
console.log(this.statusOfTheDay);
}
}
我不知道我的問題來自..這是正常的「== = HOME ===「首先出現在控制檯中?
預先感謝您的幫助,並感謝@ AJT_82。
那麼,前完成你問如何返回任何對象,其ID尚未存儲在'statuts'中並且日期匹配? – 0mpurdy
我想得到相同的結果,返回「=== STATUSTOSHOW ===」在我的組件home.ts,因爲它是空的,我不明白爲什麼 –
我認爲這是因爲你使用你的地圖錯誤 - 我現在試着寫一個答案 – 0mpurdy