Search code examples
angulartypescriptng-class

ngClass compare number with array of numbers dynamically - Angular


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]


Solution

  • 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
    }