2

How do i bind to an array in angular 11? My variable looks like

const DISH = {
        id: '0',
        name: 'Uthappizza',
        image: '/assets/images/uthappizza.png',
        category: 'mains',
        featured: true,
        label: 'Hot',
        price: '4.99',
        // tslint:disable-next-line:max-line-length
        description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
        comments: [{
            rating: 5,
            comment: 'Imagine all the eatables, living in conFusion!',
            author: 'John Lemon',
            date: '2012-10-16T17:57:28.556094Z'
        }],
}

I can bin then name like

<h3>{{dish.name | uppercase}}</h3>

But how do i bind to the comments array?

2
  • 1
    why are there so many <br />s in the code? if this is an error, please remove them. Commented Nov 22, 2020 at 16:47
  • Sorry, after long time I forgot formating. Commented Nov 22, 2020 at 17:10

1 Answer 1

2

You can show the comments while using Angular's *ngFor loop, since it's an array (list) of comments. It's described in the Angular documentation how you can use it, it's basically a for loop in your HTML. Every item within dish.comments gets looped over and item.comment (referring to dish.comments.comment) gets printed in the paragraph element.

<h3>{{ dish.name }}</h3>

<ul>
  <li *ngFor="let item of dish.comments">
    <p>{{ item.comment }}</p>
  </li>
</ul>

If there are more comments, they will show up in the list. See this StackBlitz example for the code.

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.