基于屏幕宽度的各种屏幕响应

时间:2019-03-24 15:56:05

标签: angular

我使用的是网格布局,我希望文本对视口做出响应。我正在使用此article和样式:

font-size: calc(16px + 0.5vw);

我该如何使用ts和html文件实现它?这是ts文件:

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' },
  ];
}

这是html文件:

<mat-grid-list cols="5" rowHeight="20vh" [gutterSize]="'0px'">
  <mat-grid-tile
      *ngFor="let tile of tiles"
      [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;'}"
   >
    {{tile.text}}
  </mat-grid-tile>
</mat-grid-list>

我在此行出现错误:[ngStyle] =“ {'font-size':calc(tile.fontSize +'px'+ 0.5vw);

这里是stackblitz

1 个答案:

答案 0 :(得分:2)

您的表达式无效。应该是

[ngStyle]="{'font-size': 'calc(' + tile.fontSize + 'px + 0.5vw)', 'font-family': tile.fontFamily +', sans-serif'}"

Demo