0

I have an expansion panel group and I am trying to appy a css class to the active panel:

<mdl-expansion-panel-group>
    <mdl-expansion-panel *ngFor="let task of tasks" [ngClass]="{'active': task.id == selectedTask}">
        <mdl-expansion-panel-header>
            <mdl-expansion-panel-header-list-content><h6>{{task.what_task}} {{task.id}}</h6></mdl-expansion-panel-header-list-content>
        </mdl-expansion-panel-header>
        <mdl-expansion-panel-content>
            <mdl-expansion-panel-body>

                <button mdl-button mdl-button-type="raised" mdl-colored (click)="selectTaskToEdit(task)">
                    Edit
                </button>

            </mdl-expansion-panel-body>
        </mdl-expansion-panel-content>
    </mdl-expansion-panel>
</mdl-expansion-panel-group>

css class active has a background color set to say yellow. In my component I am printing console.log(this.selectedTask==task.id) which comes out true, however my class is not being applied.

My component:

  selectTaskToEdit(task){

    this.task=task;
    this.selectedTask=task.id;
    console.log(this.selectedTask==task.id)
  }

and css:

.active {
  background-color: yellow;
}

Am I doing anything wrong?

UPDATE: I could solve it using [style.background-color]="task.id == selectedTask ? 'yellow': null " however I would like to know if there is a correct way of doing with ngClass

Thinker
  • 5,326
  • 13
  • 61
  • 137

1 Answers1

1

[style.background-color]="task.id == selectedTask ? 'yellow': null"

or

[class.active]="task.id == selectedTask"

is the only way to make it, because mdl-expansion-panel has own host class expressions, which overrides yours [ngClass].

Here is the example plnkr.

  • [class.active]="task.id == selectedTask" has no effect :( however the first approach works. I figured it out but will accept it an answer – Thinker Jul 30 '17 at 09:26