我想将我的客户表中的 init 数据加入到项目列表中。
模型是这样的:
项目
- 钥匙
- 名称:字符串
- 客户:客户密钥
顾客
- 钥匙
- 名称:字符串
你有一个例子,我如何使用angularfire2从angular2组件中做到这一点?
我的控制器看起来像这样:
import { Component, OnInit } from '@angular/core';
import { Project } from '../project';
import { Router } from '@angular/router';
import { FirebaseAuth } from 'angularfire2';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import { Observable } from 'rxjs';
@Component({
moduleId: module.id,
selector: 'app-projects',
templateUrl: 'projects.component.html',
styleUrls: ['projects.component.css']
})
export class ProjectsComponent implements OnInit {
projects: FirebaseListObservable<any[]>;
customers: FirebaseListObservable<any[]>;
projectName: string;
constructor(
private router: Router,
private af: AngularFire
) { };
ngOnInit() {
this.projects = this.af.database.list('projects');
}
add(projectName: string) {
this.af.database.list('projects')
.push({ name: projectName, id: '123' });
this.projectName = null;
}
}
更新
我已经将 this.projects 的类型从 FirebaseListObservable 更改为 Observable 我的 ngOnInit() 方法现在看起来像这样:
ngOnInit() {
this.projects = this.af.database.list(`projects`)
.map(projects => {
projects.map(project => {
this.af.database.object('customer/' + project.customer + '/name')
.subscribe(customer => {
project.customer = customer;
})
return project;
})
return projects;
});
}
我现在不能从里面的模板访问客户的名称属性
<li *ngFor="let project of projects | async">
project.customer.$value