css - 如何将TypeScript变量值设置为CSS属性?

标签 css angular typescript

我有一张图像(在 Material 卡内),每30秒更改一次。我具有图像的高度(作为TS变量),我想将其设置为已加载图像的高度。我尝试了以下方法,但我认为语法错误。什么是正确的方法?

@Component({
selector: 'app-live',
templateUrl: 

 <mat-card [ngStyle]="{'min-height': '{{taille}}'}">
  <img  mat-card-image [src]="profileUrl | async " alt="Streaming de la culture" #img [ngStyle]="{'padding-top':'0px' }" (load)="dosomething(img)">
 </mat-card>

export class LiveComponent implements OnInit {

taille;

dosomething(img) { console.log(img.clientWidth, img.clientHeight);
                   this.taille = img.clientHeight;
                   console.log(this.taille);}

最佳答案

实际上,{'min-height': '{{taille}}'}部分应该是{'min-height': taille}
documentation呈现了以下非常相似的示例:

<div [ngStyle]="{'color': colorPreference}">

从示例代码看来,taille最初是您可能需要考虑的undefined(初始化taille或使用ngIf或其他方法)。

关于css - 如何将TypeScript变量值设置为CSS属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53524279/

相关文章:

javascript - 如果有任何方法可以更改 javascript 警报消息窗口的设计和位置

html - css hover -> 更改 z-image : doesn't work in IE

javascript - Angular 2 和 C# API - 未处理的 Promise 拒绝 : ReferenceError: _body is not defined

angular - amcharts 4 光标和鼠标定位不正确

typescript - 为什么 Typescript 箭头函数泛型与命名函数泛型的工作方式不同

html - 框阴影和溢出 : scroll

Javascript OnClick 更改各种元素链接颜色

javascript - 通过 URL 检测媒体(图片、视频)类型

Angular 6目标es5 IE语法错误(箭头函数)

javascript - 如何在 Angular2 中导入 d3pie.js TypeScript 定义