I want to use [ngClass]
with a method call that returns an object like {currentWeek: true}
and I want to add the var week.state
that can have the value rejected
.
I want that the HTML looks like this: <div class="currentWeek rejected"></div>
but when I use it I get this error:
Error in ./FlexCalendar class FlexCalendar - inline template:29:13 caused by: NgClass can only toggle CSS classes expressed as strings, got [object Object]
How can I combine a method call and a variable into the ngClass? Something like [ngClass]=[setWeekClasses(week), week.state]
setWeekClasses(week: Week): Object {
let state: string = this.state(week.state);
return {
'currentWeek': week.weekNumber === this.selectedDate.getWeekNumber(),
[week.state]: true,
};
}
You can use a method and a variable to construct the classes-string for the ngClass.
For example see here a class providing a method and a object variable providing class information:
class {
getCSSClass() {
return { state: "rejected" }
}
week:object = {
state: 'rejected'
}
}
In Template then you can use a method and a variable to construct the classes-string for the ngClass.
<div [ngClass]="getCSSClass().state + ' currentWeek'">">
<h2>Hello {{name}}</h2>
</div>
or you can write:
@Component({
selector: 'my-app',
template: `
<div [ngClass]="week.state + ' ' + getCSSClass().state">
<h2>Hello {{name}}</h2>
</div>
`,
})
Check here in plunker: http://plnkr.co/edit/JfN5cjbCjNAUs8qx9oh1