繁体   English   中英

如何绑定到 angular 11 中的数组

[英]How to bind to array in angular 11

我如何绑定到 angular 11 中的数组? 我的变量看起来像

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'
        }],
}

我可以装箱然后命名为

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

但是我如何绑定到评论数组?

您可以在使用 Angular 的*ngFor循环时显示注释,因为它是一个注释数组(列表)。 在 Angular 文档中描述了如何使用它,它基本上是 HTML 中的 for 循环。 dish.comments每个item dish.comments被循环,并且item.comment (指的是dish.comments.comment )被打印在段落元素中。

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

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

如果有更多评论,它们将显示在列表中。 有关代码,请参阅此 StackBlitz 示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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