Search code examples
angularangular-ng-if

Using ng-if with comparison operator


I have created a component 'OddComponent':

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

And it's html is:

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

I am passing 'oddscore' variable from app component:

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

My motive is to use ng-if to only print odd values in the component. But it seems that ng-if does not work as expected, as it prints all the values.


Solution

  • There is no issue with the comparision, You are using angularjs syntax with angular, it should be *ngIf

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

    STACKBLITZ DEMO