如何使用 *ngIf 检查对象中是否存在元素?
.ts 文件
this.data = ["cat", "dog"];
我想在我的 html 文件中检查对象中是否存在 cat this.data
。我可以用 *ngIf 做到这一点吗?
您可以在 ts 文件中创建一个函数来检查数组中是否存在所需的字符串,如下所示:
doesExist(animal: string): boolean {
return this.data.includes(animal);
}
然后在你的 html 文件中调用它:
<div *ngIf="doesExist('cat')"> [...] </div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句