简体   繁体   中英

How to bind to array in angular 11

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM