如果有什么我可以添加来协助请告诉我。尝试获取项目的密钥,但尝试首先解决检索数据并显示它。
app.component.ts
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
coursesRef: AngularFireObject<any>;
courses: Observable<any>;
constructor(db: AngularFireDatabase) {
this.coursesRef = db.object('/courses');
this.courses = this.coursesRef.valueChanges();
this.courses.subscribe(course => console.log(course));
}
}
app.component.html
<ul>
<li *ngFor="let course of courses | async">
{{ course | json }}
</li>
</ul>
这会返回4个对象,尽管数据库只包含3个。
答案 0 :(得分:0)
我相信答案是应该在ngOnInit函数中调用该对象。
app.component.ts
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
coursesRef: AngularFireList<any>;
courses: Observable<any[]>;
constructor(private db: AngularFireDatabase) {
this.coursesRef = db.list('/courses');
}
ngOnInit() {
this.courses = this.coursesRef.valueChanges();
this.courses.subscribe(course => console.log(course));
}
}