在Angular Material中根据条件应用两个不同的面板类。

huangapple go评论55阅读模式
英文:

Apply two different classes with panel class depending on the condition in Angular material

问题

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 it's not working and in DOM showing complete things inside "" as a string.

I have attached a screenshot of generated HTML.

英文:

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.

&lt;mat-select panelClass=&quot;customClass ? customClass : select-position&quot;&gt; 
&lt;/mat-select&gt;

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

在Angular Material中根据条件应用两个不同的面板类。

答案1

得分: 2

Sure, here's the translated part:

你可以尝试使用方括号和类名用引号括起来的记法,像这样:

&lt;mat-select [panelClass]=&quot;customClass ? &#39;customClass&#39; : &#39;select-position&#39;&quot;&gt; &lt;/mat-select&gt;
英文:

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

&lt;mat-select [panelClass]=&quot;customClass ? &#39;customClass&#39; : &#39;select-position&#39;&quot;&gt; &lt;/mat-select&gt;

答案2

得分: 1

Sure, here's the translated content:

  1. 使用属性绑定(用方括号[]包裹panelClass)
<mat-select [panelClass]="customClass ? 'customClass' : 'select-position'">
</mat-select>
  1. 使用插值(用{{}}包裹条件)
<mat-select panelClass="{{ customClass ? 'customClass' : 'select-position' }}">
</mat-select>
英文:

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

  1. Using property binding (Wrapping the panelClass with square brackets [])
&lt;mat-select [panelClass]=&quot;customClass ? &#39;customClass&#39; : &#39;select-position&#39;&quot;&gt; 
&lt;/mat-select&gt;
  1. Using interpolation (Wrapping the condition in {{}})
&lt;mat-select panelClass=&quot;{{ customClass ? &#39;customClass&#39; : &#39;select-position&#39; }}&quot;&gt; &lt;/mat-select&gt;

huangapple
  • 本文由 发表于 2023年5月17日 14:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定