-2

I have something like this:

[ngClass]="{className: singleNumber == arrayOfNumbers}

How do I compare 1 === [1,2,3,4] ? it works if I do this: arrayOfNumbers[0]

Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93
  • Your condition is not clear. When should the class be applied? When `arrayOfNumbers` contains `singleNumber`, or when `arrayOfNumbers` contains only `singleNumber`? – ConnorsFan Jan 27 '18 at 21:49

2 Answers2

1

Why not make it easier for yourself and have the comparison in the ts and check the bool on template?

HTML

[class.className]="isInArray"

TS

arrayOfNumbers = [1,2,3,4];
//in some part where you want to trigger the check
foo(myNumber: number) {
  this.isInArray = this.arrayOfNumbers.indexOf(myNumber) !== -1
}
Mike Tung
  • 4,735
  • 1
  • 17
  • 24
0

This is one way to handle your situation

[ngClass] ="{ className : arrayOfNumbers.includes(singleNumber) }

This function's documentation can be found here

Nello
  • 1,737
  • 3
  • 17
  • 24