我有一张图像(在 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/