2

I have a div with a ngClass condition:

<div [ngClass]="{ 'active': nbActive === 1 }" >
    <!-- some stuff -->
</div>

And a similar div with a ngIf condition:

<div *ngIf="nbActive === 1">
    <!-- some stuff -->
</div>

Below is the NbActive declaration:

export class WhyChooseUsComponent implements OnInit {
   nbActive: 0;
   constructor() { }
   // some stuff
}

If in my production configuration, I set:

"aot": true,
"buildOptimizer": true,

Then I get the following error:

This condition will always return 'false' since the types '0' and '1' have no overlap.

I don't get any error if I set aot and buildOptimizer to false and everything is working as expected.
Where does this issue come from and how can I fix this?

PMerlet
  • 2,568
  • 4
  • 23
  • 39

2 Answers2

2

You wrote

   nbActive: 0;

Which means the only acceptable value for nbActive is 0.

You might needed this instead:

   nbActive: number = 0;
Abel
  • 688
  • 6
  • 19
0

you can try like this


<div [ngClass]="isActive === 1 ? 'active': '' " >
    <!-- some stuff -->
</div>

<div *ngIf="isActive">
    <!-- some stuff -->
</div>
Yash Rami
  • 2,276
  • 1
  • 10
  • 16