Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ngStyle only on specific condition

<h4 *ngFor="let x of textAddedList" [ngStyle]="{backgroundColor:textAddedList.length>2?'grey':'transparent'}">{{x}}</h4>

I want to apply the grey styling only after the 2 entery, but when I applying this logic it apply to all. instead after 2nd line. Thanks in advance.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Work with index in *ngFor. As the index of array starts with 0, for the rows after the second row, the index are greater than 1:

<h4
  *ngFor="let x of textAddedList; let i = index"
  [ngStyle]="{ backgroundColor: i > 1 ? 'grey' : 'transparent' }"
>
  {{ x }}
</h4>

Sample StackBlitz Demo

Output

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading