[英]How can I add a custom class to an Angular Material tab?
In Angular 6, I am using Angular Material to display some data using Tabs.在 Angular 6 中,我使用 Angular Material 来使用 Tabs 显示一些数据。
<mat-tab-group>
<mat-tab
*ngFor="let bar of foo.bar"
[label]="bar.Name"
[ngClass]="bar.IsActive ? 'bar-on' : 'bar-off'">
// ...
</mat-tab>
</mat-tab-group>
I would like to style the tabs differently whether the IsActive
property of bar is true
or false
.无论 bar 的IsActive
属性是true
还是false
,我都想以不同的方式设置选项卡的样式。
I have tried using [ngClass]="bar.IsActive? 'bar-on': 'bar-off'"
as shown above, but the class is not added.我已尝试使用[ngClass]="bar.IsActive? 'bar-on': 'bar-off'"
如上所示,但未添加 class。
Out of spite, I have tried the much simpler class = 'bar-on'
but even then the class is not added.出于恶意,我尝试了更简单的class = 'bar-on'
但即使那样也没有添加 class 。
Therefore, I am wondering, is it possible to add a custom class to an Angular Material tab?因此,我想知道,是否可以将自定义 class 添加到 Angular 材料选项卡?
Edit: The IsActive
property is a property of foo
objects.编辑: IsActive
属性是foo
对象的属性。 It has nothing to do with the activated state of the tab.与tab激活的state无关。 The active tab can display an object with IsActive = false
and vice-versa.活动选项卡可以显示 object 且IsActive = false
,反之亦然。
Yes its possible by using ng-template
instead of label
.是的,可以通过使用ng-template
而不是label
。
Example of ngClass
on first tab.第一个选项卡上的ngClass
示例。 stackblitz 闪电战
<mat-tab-group>
<mat-tab >
<ng-template mat-tab-label>
<span [ngClass]="{'color-red': isError?'red':'black'}">Security</span>
</ng-template>
Content 1
</mat-tab>
<mat-tab label="Second"> Content 2 </mat-tab>
<mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>
If you want to modify the active tab, use this:如果要修改活动选项卡,请使用以下命令:
.mat-tab-label-active {
//your style here
}
If you want to modify the label style within the active tab, do this:如果要修改活动选项卡中的标签样式,请执行以下操作:
.mat-tab-label-active .mat-tab-label-content {
//your style here
}
The API allows you to conditionnally set the color
and background-color
of the whole mat-tab-group
but not to an individual mat-tab
. API允许您有条件地设置整个mat-tab-group
的color
和background-color
,但不能设置为单独的mat-tab
。
You can't do what you want to achieve unless you do some hacky native javascript... 除非你做一些hacky原生javascript,你不能做你想要实现的目标......
或者你可以使用 ::ng-deep,谷歌它。
Target the element by attribue to apply style通过属性定位元素以应用样式
:host::ng-deep .mat-tab-label[aria-posinset='1']{
/*apply your css here*/
}
Seems like the <mat-tab>
element gets removed, so if you want to add a CSS class to it, you can just wrap the tab contents in another div and add the class to that div.似乎<mat-tab>
元素被删除了,所以如果你想向它添加 CSS class,你可以将选项卡内容包装在另一个 div 中并将 class 添加到该 div。
I am using Angular 11 atm, and the label class feature was introduced later, so I have to do it differently.我用的是Angular 11 atm,后来又介绍了label class这个功能,只好另辟蹊径了。
But generally, with Angular 13+, this should be possible by using the @Input [labelClass]="my-classname"
.但一般来说,对于 Angular 13+,这应该可以通过使用 @Input [labelClass]="my-classname"
来实现。 https://material.angular.io/components/tabs/api https://material.angular.io/components/tabs/api
And instead of ::ng-deep
, better use encapsulation: ViewEncapsulation.none
, if you need to style elements from 3rd party imports.而不是::ng-deep
,更好地使用encapsulation: ViewEncapsulation.none
,如果你需要从 3rd 方导入元素样式。
Instead of using [ngClass] for highlighting an active tab, you could ..而不是使用 [ngClass] 突出显示活动选项卡,您可以..
.mat-tab-label-active{
//your code...
}
See this : https://stackoverflow.com/a/45962498/5319180看到这个: https : //stackoverflow.com/a/45962498/5319180
Also : https://github.com/angular/material2/issues/5014另外: https : //github.com/angular/material2/issues/5014
EDIT : to get the domain specific info, as you said..maybe this would help?编辑:如您所说,要获取特定于域的信息……也许这会有所帮助?
<mat-tab *ngFor="let bar of bars">
<ng-template mat-tab-label>
<div [ngClass]="...your conditions">
bar.heading
</div>
</ng-template>
...
...
</mat-tab>
In Angular 6, I am using Angular Material to display some data using Tabs.在Angular 6中,我使用Angular Material通过Tabs显示一些数据。
<mat-tab-group>
<mat-tab
*ngFor="let bar of foo.bar"
[label]="bar.Name"
[ngClass]="bar.IsActive ? 'bar-on' : 'bar-off'">
// ...
</mat-tab>
</mat-tab-group>
I would like to style the tabs differently whether the IsActive
property of bar is true
or false
.无论bar的IsActive
属性为true
还是false
我都希望为标签设置不同的样式。
I have tried using [ngClass]="bar.IsActive ? 'bar-on' : 'bar-off'"
as shown above, but the class is not added.我已尝试使用[ngClass]="bar.IsActive ? 'bar-on' : 'bar-off'"
,但未添加该类。
Out of spite, I have tried the much simpler class = 'bar-on'
but even then the class is not added.尽管如此,我还是尝试了更简单的class = 'bar-on'
但即使如此,该类也没有添加。
Therefore, I am wondering, is it possible to add a custom class to an Angular Material tab?因此,我想知道是否可以向Angular Material选项卡添加自定义类?
Edit: The IsActive
property is a property of foo
objects.编辑: IsActive
属性是foo
对象的属性。 It has nothing to do with the activated state of the tab.它与选项卡的激活状态无关。 The active tab can display an object with IsActive = false
and vice-versa.活动选项卡可以显示IsActive = false
的对象,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.