html - Angular 7手动设置背景颜色平铺

标签 html angular angular-material2

这是我的代码:

<mat-grid-list cols="4" rowHeight="100px">
    <mat-grid-tile colspan="2" rowspan="2">
        1
    </mat-grid-tile>
    <mat-grid-tile colspan="2" rowspan="2">
        2
    </mat-grid-tile>
    <mat-grid-tile colspan="1" rowspan="1">
        3
    </mat-grid-tile>
    <mat-grid-tile colspan="1" rowspan="1">
        4
    </mat-grid-tile>
    <mat-grid-tile colspan="2" rowspan="1">
        5
    </mat-grid-tile>
</mat-grid-list>

我想为这些图 block 手动应用背景颜色。 我知道我可以使用这个方法:_setStyle(property: string, value: any): void;

但我不知道我的情况如何,因为我没有在我的组件中初始化任何 MatGridTile 对象。我不想使用动态填充的网格。

https://stackblitz.com/edit/angular-adjmya?file=app/grid-list-dynamic-example.html

最佳答案

[style.background] 中尝试 "'red'"

<mat-grid-list cols="4" rowHeight="100px">
    <mat-grid-tile colspan="2" rowspan="2" [style.background]="'red'">
        1
    </mat-grid-tile>
    <mat-grid-tile colspan="2" rowspan="2">
        2
    </mat-grid-tile>
    <mat-grid-tile colspan="1" rowspan="1">
        3
    </mat-grid-tile>
    <mat-grid-tile colspan="1" rowspan="1">
        4
    </mat-grid-tile>
    <mat-grid-tile colspan="2" rowspan="1">
        5
    </mat-grid-tile>
</mat-grid-list>

Demo code here

更新:

它需要 ' 单引号的原因是因为根据 demo ,我们可以看到它期望

[style.background]="tile.color"

这表明它采用 angular 变量并对其进行插值。通过提供 ' ,您告诉 ma​​terial component 它是一个字符串,而不是 component 中定义的某个 Angular 变量。否则,使用 style.backgroundColor="red" ,它会在组件中查找名为 red 的变量。

关于html - Angular 7手动设置背景颜色平铺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55472155/

相关文章:

html - 当字段是动态的时如何在表格宽度内包含标题

php - 上传 CSV 文件,PHP/MySQL 查询

javascript - 页面重新加载后按钮更改

angular - 为什么 Angular CLI 不会自动在提供者数组中添加服务?

javascript - 下拉菜单中改变线宽

html - 滚动Angular2时锁定一个div

css - 动画星云侧边栏组件

angular - XHR 错误 (404 Not Found) 加载 http ://localhost:3000/traceur

Angular 5 : Dynamic component loading - StaticInjector error with @angular/core dependencies

css - Angular2 md-select 选择选项后删除箭头