Angular 无效参数:'';对于管道';异步管道&x27;
行内:Angular 无效参数:'';对于管道';异步管道&x27;,angular,angular5,angular8,Angular,Angular5,Angular8,行内:index:this.timers.length来自管道上的角度文档: async管道订阅一个Observable或Promise并返回它发出的最新值。当发出一个新值时,async管道会标记要检查的组件的更改。当组件被销毁时,async管道自动取消订阅以避免潜在的内存泄漏 但是从您的属性声明来看,它似乎是一个数组。所以它不会起作用 不要在此处初始化计时器属性,并将计时器的类型更改为可观察 大概是这样的: add() { let options = { finishDa
index:this.timers.length
来自管道上的角度文档:
async
管道订阅一个Observable
或Promise
并返回它发出的最新值。当发出一个新值时,async
管道会标记要检查的组件的更改。当组件被销毁时,async
管道自动取消订阅
以避免潜在的内存泄漏
但是从您的属性声明来看,它似乎是一个数组
。所以它不会起作用
不要在此处初始化计时器
属性,并将计时器
的类型更改为可观察
大概是这样的:
add() {
let options = {
finishDate: null,
time: null,
index: this.timers.length
};
this.timers.push(this.create(options));
}
您只是在错误的位置使用了async
管道。试试这个:
timers: Observable<ITimer>[]=[];
然后,您可以在模板中使用:
add() {
let options = {
finishDate: null,
time: new Date(),
index: this.timers.length
};
this.timers.push(this.create(options));
this.timersArray.push(options);
this.timers$.next(this.timersArray);
}
{{item.time}
去除
这是给你裁判的一封信
如果不初始化,我无法执行此操作:
this.timers.length
它会出现错误。请使用相关代码更新您的问题,并提供更多有关您在此处尝试执行的操作的信息?这将帮助我理解用例,然后我可能会建议一些解决方案:)完成,请参阅问题
timers: Observable<ITimer[]>;
timers: Observable<ITimer>[]=[];
<div *ngFor="let item$ of timers">
<div *ngIf="(item$ | async) as item">
{{ item.time }}
<div (click)="remove(item.index)">Remove</div>
</div>
</div>
timersArray: ITimer[] = [];
timers$: BehaviorSubject<Array<ITimer>> = new BehaviorSubject<Array<ITimer>>(this.timersArray);
add() {
let options = {
finishDate: null,
time: new Date(),
index: this.timers.length
};
this.timers.push(this.create(options));
this.timersArray.push(options);
this.timers$.next(this.timersArray);
}
<div *ngFor="let item of (timers$ | async)">
{{ item.time }}
<button (click)="remove(item.index)">Remove</button>
</div>