Add css style after element loads in Angular

Viewed 395

I have a function that is used to calculate the left margin which will be assigned to list elements. The issue is that the id for the list element is generated when the page loads and has a binding to it:

<li *ngFor "let msg in messages; let i in index;" id="#messages-{{i}}"> ... </li>

How can I use my function to assign the margin to these list contents? Every li element will have different margins.

1 Answers

In your HTML Template

<li *ngFor "let msg in messages; let i in index;" 
[style.marginTop.px]="getMarginLeft(msg)" id="#messages-{{i}}"> ... </li>

In your Ts file

  getMarginLeft(msg) {
    return msg.length;
  }
Related