0

I have:

<div *ngFor="let item of items">
  <h2>Hello {{item.name}}</h2>
</div>

My items:

items = [
    {
      name: 'David',
      star: 5
    },
    {
      name: 'George',
      star: 2
    },
    {
      name: 'Michael',
      star: 0
    },
    {
      name: 'Tim',
      star: 1
    },
]

They render:

Hello Tim

etc.

How to add:

<i class="fa fa-star"></i>

above h2 when in array:

star: 1

etc? Something like this:

if (this.items.star == 2) {
   <i class="fa fa-star"></i>
   <i class="fa fa-star"></i>
}

(It's just an example "if statement" to understand what's going on)

My PLUNKER: https://plnkr.co/edit/lfZT6FwenhYkQf1MUx9E?p=preview

1 Answer 1

1

You can use the *ngIf template decorator this way:

<div *ngFor="let item of items">
  <i class="fa fa-star" *ngIf="item.star === 1></i>
  <h2>Hello {{item.name}}</h2>
</div>

See more examples and read more about it here

*ngIf: Conditionally includes a template based on the value of an expression.

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.