*ngIf
当指令基于异步条件时,如何摆脱上述错误?
在我的主要组件 ( products.ts
) 中,我订阅了一个 observable,这是用户选择的结果,他可以通过与许多组件(包括products.ts
)交互来实现。在此组件的模板 ( products.html
) 中,我需要检查是否选择了任何产品,如果是,则显示它们的编号。
这一切都很完美,但我想摆脱这个错误。
产品.ts
ngOnInit() {
this.productService.getProducts().subscribe(data => {
this.products = data;
this.countProducts = this.products.length;
});
}
产品.html
<span *ngIf="countProducts" [matBadge]="countProducts"></span> //if countProduct is not 0 or undefined, display the number of products