首页 > 解决方案 > 使用带有比较运算符的 ng-if

问题描述

我创建了一个组件“OddComponent”:

export class OddComponent implements OnInit {
  @Input() oddscore = 0;
  constructor() { }
  ngOnInit() {
  }
}

它的html是:

<div ng-if="oddscore %2 ==0 ">
<p>
  odd works! {{oddscore}}
</p>
</div>

我正在从应用程序组件传递 'oddscore' 变量:

<app-odd [oddscore]="gameScore">
</app-odd>

我的动机是使用 ng-if 仅在组件中打印奇数值。但似乎 ng-if 无法按预期工作,因为它会打印所有值。

标签: angularangular-ng-if

解决方案


比较没有问题,您使用 angularjs 语法和 angular,应该是*ngIf

<div *ngIf="oddscore%2 ==0 ">

STACKBLITZ DEMO


推荐阅读