简体   繁体   English

如何在 Angular Material List an Item 组件中使用

[英]How to use in Angular Material List an Item component

I'm trying to migrate Angular code from Bootstrap to Material design.我正在尝试将 Angular 代码从 Bootstrap 迁移到 Material design。 I need to show a list of items (for example Cars).我需要显示一个项目列表(例如汽车)。 I have a car-list component and car-item component.我有一个汽车列表组件和汽车项目组件。 I want to keep it separate if it's possible.如果可能的话,我想把它分开。

Unfortunately the resulting list of 10 items is presented as 10 square boxes on one row instead of expected 10 rows.不幸的是,10 个项目的结果列表显示为一行上的 10 个方形框,而不是预期的 10 行。

My question is: Can I use a separate component inside a <mat-list-item> ?我的问题是:我可以在<mat-list-item>使用单独的组件吗?

All angular material examples I came across showed mat-list-item with ngFor directive and item template code inside node.我遇到的所有 angular 材料示例都显示了带有 ngFor 指令的 mat-list-item和节点内的项目模板代码。 That means to move Item functionality into a List.这意味着将项目功能移动到列表中。

Bootstrap version引导版本

car-list-component.html汽车列表组件.html

<div class="row">
  <div class="col-xs-12">
    <router-outlet></router-outlet>
    <app-car-item
      *ngFor="let carItem of cars"
      [car] = "carItem"
    ></app-car-item>
  </div>
</div>

car-item.component.html car-item.component.html

<div>
  <div>
    <a (click)="carDetail(car.id)">
      <span>{{ getMakeLabel(car.make)}} : {{car.carModel.modelName}} </span>
    </a>
  </div>
  <div>
    <a (click)="remove(car)"><i class="material-icons">delete</i>
    </a>
  </div>
</div>

My Angular Material version with a separate item template我的 Angular 材质版本,带有单独的项目模板

car-list.component.html car-list.component.html

<div>
<mat-list role="list">
  <mat-list-item>
    <router-outlet></router-outlet>
    <app-car-item
      *ngFor="let carItem of cars"
      [car] = "carItem"
    ></app-car-item>
  </mat-list-item>
</mat-list>
</div>

car-item.component.html does not change. car-item.component.html没有改变。

Angular Material Example with 'inline' item template. Angular 带有“内联”项目模板的材料示例。

This is presented in all examples and tutorials on Angular Material.这在 Angular 材料的所有示例和教程中都有介绍。 It would work for me, but as I said before if Item(car) is a more complex object than in this example then I want to keep it separately.它对我有用,但正如我之前所说,如果 Item(car) 是一个比本示例更复杂的 object,那么我想单独保留它。

car-list.component.html car-list.component.html

<mat-list>
    <mat-list-item *ngFor="let car of cars">
      <a style="cursor: pointer;" (click)="carDetail(car.id)">
       <span>{{ getMakeLabel(car.make)}} : {{car.carModel.modelName}} </span>
      </a>
    </mat-list-item>
</mat-list>

You can add mat-list-item as the directive to your app-car-item component.您可以将 mat-list-item 作为指令添加到您的 app-car-item 组件。 please refer https://stackblitz.com/edit/angular-blzeb1请参考https://stackblitz.com/edit/angular-blzeb1

<mat-list role="list">
    <app-car-item mat-list-item
      *ngFor="let carItem of cars"
      [car] = "carItem"
    ></app-car-item>
 </mat-list>

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

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