使用角度网格,我想为网格的每个部分创建一个类,以在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'”
答案 0 :(得分:2)
要使用ngFor
索引设置类名,请使用ngClass:
<mat-grid-tile
*ngFor="let tile of tiles; let i = index"
[ngClass]="'tile' + i"
...
>
有关演示,请参见this stackblitz。