1

So within my *ngfFor loop I have this

[ngClass]="{'first':isStartDate(event,day)}"

where isStartDate is a function in my component.

error thrown is

"Unexpected token : "

Is it possible to to use a function here?

2
  • [class.first]="ifStartDate(event,day)" Commented May 4, 2017 at 10:50
  • your issue resolve or not ? @user3057416 Commented May 4, 2017 at 11:15

5 Answers 5

4

ifStartDate function is return only boolean value

[class.first]="ifStartDate(event,day)"
Sign up to request clarification or add additional context in comments.

Comments

1

You can also give dynamic class as below:

[ngClass]="ifStartDate(event,day) ? 'first' : ''"

Comments

1

Best way to implement it:

<div [ngClass]="ifStartDate(event,day)">
</div>

ifStartDate(event, day) {
    let cssClasses;

    if(some condition) {  
      cssClasses = {
       'first': true
      } 
    } else {  
      cssClasses = {
       'second': true,
       'third': true 
      } 
    }

    return cssClasses;
}

In this way you can also apply multiple classes over div based on some condition.

Comments

0

Yes you can definitely use function in ngClass

[ngClass]="{first: isStartDate(event,day)}"

Comments

0

You can use expression in ngClass:

[ngClass]="'{first:'+ifStartDate(event,day)+'}'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.