3

I have an image (inside a material card) which changes every 30 seconds. I have the height of the image (as a TS variable) and I want to set it as the height of the loaded image. I tried the below approach but I think that the syntax is wrong. What is the right approach?

@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);}
2
  • Remove {{ and }} Commented Nov 28, 2018 at 16:53
  • better said remove '{{ and }}' around taille Commented Nov 28, 2018 at 18:12

2 Answers 2

5

The {'min-height': '{{taille}}'} part should in fact be {'min-height': taille}.
Angular documentation presents the below very similar example:

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

From your sample code it seems that taille is initially undefined which you might want to take into account (initialize taille or use a ngIf or other approach).

Sign up to request clarification or add additional context in comments.

Comments

3

Using [ngStyle] -

<mat-card [ngStyle]="{'min-height': taille}">

Or you can use [style.min-height] = "taille" instead.

<mat-card [style.min-height] = "taille">

please go through this for more insights.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.