利用这里的信息:Angular 2 change detection with observables我的Observable正常工作以进行初始加载 . 但是,当我添加新项目时,现有项目将被清除,只剩下新添加的项目和"blank"项目 .
这是我的添加项功能:
public add(v : any){
this.db.insert(v,
(e, nDoc) => {
if(!e){
this.ngZone.run(() => this.sub.next([this.sub.getValue(), nDoc]));
} else{
console.log(e);
}})
}
该项目正确地添加到数据库中(使用nedb),但是在UI中显示项目的绑定显示空项目和新添加的项目以及每个项目的复选框 .
这是我的约束力:
<ul>
<li *ngFor = 'let todo of items | async'>
<input type='checkbox' title='{{todo.description}}' /><span class='todoTitle' title='{{todo.description}}'>{{todo.title}}</span>
</li>
</ul>
这是我的订阅和添加新项目的调用:
this.items = this._todoService.items;
this.items.subscribe({
next: x => console.log("got value:", x),
error: e => console.error("observable error" , e),
complete: () => console.log("done")
});
setTimeout(() => {
this._todoService.add({"title":"NEW","priority":1,"status":"open","_id":new Date().getTime(),"description":""});
}, 3000);
在控制台中, next
方法记录 got value [Array[6], Object]
所以看起来服务没有返回我的项目的单个数组,而是返回现有项目的新数组(在UI中表示为"blank"),然后是新的添加项目 .
如何让它只是所有项目的单个数组,以便正确显示?
谢谢 .