3

I have below HTML code which recursively create list items based on list returned from Component and I want to apply 'first-child' CSS class to first List item only.

<ul class="link-list-horz">
<li *ngFor="let menu of menulist" [ngClass]="first-child:">
 <a href="">{{menu}}</a>
</li>
</ul>

.first-child a 
{
    border-radius: 10; 
}

export class AppComponent  { 
   name = 'Quiz'; 
   menulist = ['Home','AngularQuiz'] ;  
   useremailid = 'Gaurav-Gupta'; 
}

Please suggest. I am totally new to Angular2.

1 Answer 1

2

ngClass needs a condition to know whether to set that class on the element. You can use the built-in index that comes with ngFor for that.

Try this:

<li *ngFor="let menu of menulist; let i=index" [ngClass]="{'first-child': i === 0}">
Sign up to request clarification or add additional context in comments.

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.