【发布时间】:2017-10-31 15:06:59
【问题描述】:
使用 AngularFireStrore 我得到一个集合并将其分配给一个可观察的数组。然后在由按钮激活的功能中,我想修改该集合。我正在尝试按以下方式进行操作,但它向我表明 vaor 是一种可观察的类型。
private listBonosImponibles: AngularFirestoreCollection<EmployeeBonos>;
unBonosImponibles: Observable<EmployeeBonos[]>;
constructor(private afs: AngularFirestore, private route: ActivatedRoute) {
this.listBonosImponibles = this.itemDoc.collection<EmployeeBonos>('bonosImponibles');
this.unBonosImponibles =
this.listBonosImponibles.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as EmployeeBonos;
const id = a.payload.doc.id;
return { id, ...data };
});
});
}
我在构造函数中成功获取了我的集合,现在如果我想显示通过控制台获得的数组,它不会显示任何内容,因为它没有进入 console.log 在其中添加一个断点并且它没有进入
saveEmployee(){
this.unBonosImponibles.map((dat: any) => {
console.log(dat);
this.listBonosImponibles.doc(dat.id).update(dat);
});
}
这就是我在视图中显示矩阵的方式
<div *ngFor="let bonoImpo of unBonosImponibles | async" fxLayout="row" >
<label fxFlex="70" >{{ bonoImpo.nombre }}</label>
<mat-form-field class="ml-5" >
<input matInput [(ngModel)]="bonoImpo.valor" />
</mat-form-field>
</div>
<button mat-raised-button (click)="saveEmployee()" color="primary">Guardar</button>
【问题讨论】:
-
在你的例子中
vaor在哪里?请给出一致的minimal reproducible example(最好是英文,这样更容易理解应该发生的事情)。一般来说,如果你想访问一个可观察数组中的数组,请使用像.map这样的可观察运算符。 -
@jonrsharpe 现在修改代码,如果我将地图添加到可观察的控制台日志里面没有运行
-
您需要在某处订阅它。如果你没有返回 observable,只需将
map替换为subscribe。您实际上并没有尝试来操作数组。 -
如果您能阐明修改集合的目的,将会有所帮助。您是否尝试通过 AngularFirestore 保留更改?您如何从视图中获得更改?
-
@bygrace 我想要的是遍历集合,然后获取每个文档的 id 并更新该集合中的每个文档
标签: angular typescript rxjs angularfire2