0

I am using mat-select of Angular material. It has panelclass property that can be used to apply class. In parent component I have to style the drop down. The issue is :- I have to apply dynamic class to panelclass but type of panel class is string.

<mat-select panelClass="customClass ? customClass : select-position"> 
</mat-select>

I want to do something like this but its is not working and in DOM showing complete things inside "" as string

I have attcahed screenshot of generated HTML

enter image description here

Nikita
  • 682
  • 2
  • 13
Apurva Pathak
  • 692
  • 1
  • 9
  • 22
  • did you try [ngCLass](https://angular.io/api/common/NgClass) ? – Kaneki21 May 17 '23 at 06:52
  • 1
    Just wrap the `panelClass` in the [square brackets](https://angular.io/guide/property-binding#binding-to-a-property), otherwise right-hand side won't be treated as expression but as a string. e.g. `[panelClass]="isCustom ? 'custom' : 'select-position'"` – TotallyNewb May 17 '23 at 07:05
  • Yes that was the issue Thanks :) – Apurva Pathak May 18 '23 at 05:31

2 Answers2

2

Could you try the notation with the square brackets and the class names in quotes like this:

<mat-select [panelClass]="customClass ? 'customClass' : 'select-position'"> </mat-select>
Leonardum
  • 444
  • 4
  • 8
1

You can use it in 2 ways to bind the values:

  1. Using property binding (Wrapping the panelClass with square brackets [])
<mat-select [panelClass]="customClass ? 'customClass' : 'select-position'"> 
</mat-select>
  1. Using interpolation (Wrapping the condition in {{}})
<mat-select panelClass="{{ customClass ? 'customClass' : 'select-position' }}"> </mat-select>
Srikar Phani Kumar M
  • 1,069
  • 1
  • 3
  • 8