angular - 使用 *ngFor 创建类
问题描述
使用角度网格我想为网格的每个部分创建一个类以在 css 文件中使用。这是html文件:
<mat-grid-list cols="5" rowHeight="20vh" [gutterSize]="'0px'">
<mat-grid-tile
*ngFor="let tile of tiles; let i of [1,2,3,4,5]"
[colspan]="tile.cols"
[rowspan]="tile.rows"
[style.background]="tile.color"
[ngStyle]="{'font-size': 'calc(' + tile.fontSize + 'px + 0.5vw)',
'font-family': tile.fontFamily +', sans-serif;'}"
class="tile + 'i'"
>
{{tile.text}}
</mat-grid-tile>
</mat-grid-list>
这是组件文件:
import { Component } from '@angular/core';
export interface Tile {
color: string;
cols: number;
rows: number;
text: string;
fontSize: string;
fontFamily: string;
}
@Component({
selector: 'grid-list-dynamic-example',
templateUrl: 'grid-list-dynamic-example.html',
styleUrls: ['grid-list-dynamic-example.css'],
})
export class GridListDynamicExample {
tiles: Tile[] = [
{ text: 'One', cols: 5, rows: 1, color: 'lightblue', fontSize: '20',
fontFamily: 'Rubik'},
{ text: 'Two', cols: 3, rows: 1, color: 'lightgreen', fontSize: '20',
fontFamily: 'Roboto Condensed'},
{ text: 'Three', cols: 2, rows: 2, color: 'lightpink', fontSize:
'25', fontFamily: 'Rubik' },
{ text: 'Four', cols: 3, rows: 1, color: '#DDBDF1', fontSize: '30' ,
fontFamily: 'Roboto Condensed' },
{ text: 'Five', cols: 3, rows: 1, color: '#DDBDF1', fontSize: '35',
fontFamily: 'Roboto Condensed'},
{ text: 'Six', cols: 2, rows: 1, color: '#DDBDF1', fontSize:
'40',fontFamily: 'Rubik' },
];
我认为这条线不准确:class="tile + 'i'"
这是堆栈闪电战
解决方案
要使用索引设置类名ngFor
,请使用ngClass:
<mat-grid-tile
*ngFor="let tile of tiles; let i = index"
[ngClass]="'tile' + i"
...
>
请参阅此 stackblitz以获取演示。
推荐阅读
- asp.net-core - sendgrid 如何在没有电子邮件密码的情况下工作?
- apache-spark - rdd中的子字符串使用pyspark
- material-ui - 更改 MUI TextField 边框颜色
- regex - 正则表达式 - 匹配 3 位数字
- python - 如何检查用户输入的 str 值是否等于从 pandas 数据集填充的列表中的任何 str 值?
- java - 如何设置变量数组值
- c# - 我已将操作的安装执行顺序提供为 After="InstallInitialize",但是它在卸载时运行。如何防止它?
- c# - 自定义页面过滤器中的 ASP .NET Core 注入服务
- git - 我可以让“git add -p”在添加大块后立即退出吗?
- react-native - 如何从控制台登录中删除警告本机反应