我有两个组成部分。一个是卡片仪表板(使用 ng生成@ angular / material:material-dashboard --name = my-dashboard 创建),其中卡片的创建方式如下:
<div class="grid-container">
<h1 class="mat-h1">Dashboard</h1>
<mat-grid-list cols="2" rowHeight="350px">
<mat-grid-tile *ngFor="let card of cards" [colspan]="card.cols" [rowspan]="card.rows">
<mat-card class="dashboard-card">
<mat-card-header>
<mat-card-title> {{card.title}}
<button mat-icon-button class="more-button" [matMenuTriggerFor]="menu" aria-label="Toggle menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu" xPosition="before">
<button mat-menu-item>Expand</button>
<button mat-menu-item>Remove</button>
</mat-menu>
</mat-card-title>
</mat-card-header>
<mat-card-content class="dashboard-card-content">
<div>{{ card.content }}</div>
</mat-card-content>
</mat-card>
</mat-grid-tile>
</mat-grid-list>
</div>
并且像那样填充:
export class DashComponent {
cards = [
{ title: 'Character', cols: 2, rows: 1, content: '<app-character-sheet></app-character-sheet>' },
{ title: 'Card 2', cols: 1, rows: 1 },
{ title: 'Card 3', cols: 1, rows: 2 },
{ title: 'Card 4', cols: 1, rows: 1 }
];
}
我想用不同的组件填充卡片。但是,当我去尝试它时,你可以看到上面(使用内容变量),HTML没有解析选择器。这张卡只是字面上的内容:。
如何使用组件动态填充卡片?
干杯
答案 0 :(得分:1)
如果您确实需要动态插入组件,请阅读this文章。
替代解决方案是在您拥有有限的内容选项集的情况下使用ngIf或ngSwitchCase等结构指令:
<mat-card-content class="dashboard-card-content">
<div *ngIf="card.content"><app-character-sheet></app-character-sheet></div>
</mat-card-content>
如果您只是需要不安全地插入HTML而不进行后期处理:
<div [innerHTML]="card.content"></div>