I want to bind a result of a function inside my [ngClass] condition, but it's simply not doing anything.

HTML

<div [ngClass]="{hover': check(token.ids)}" *ngFor="let tokens of info.data">  

I trying to return a boolean from the function and nothing happens, but When I log it inside the console I receiving true or false value depending on the passed elements.

check(tokens) {
    tokens.forEach(token => {
      if (token.id === this.currentToken) {
        return true
      }
    });
  } 

2 Answers

0
Friso Hoekstra On Best Solutions

Short circuit Array.forEach like calling break

Try this

  check(tokens) {
      const match = false;
      tokens.forEach(token => {
        if (token.id === this.currentToken) {
          match = true
        }
      });
      return match;
   } 
1
hafridi On
 check(tokens) {
    let res = false;
    tokens.forEach(token => {
       if (token.id === this.currentToken) {
         res = true
       }
    });
    return res;
 } 

Your check function is not returning a value and it will return undefined as a value. So you could do it this way where you are returning back value by declaring it in the function..