簡體   English   中英

角度多行列表單擊任意位置進行導航

[英]Angular multi-line list click anywhere to navigate

因此,這可能很簡單,但我很難創建一個包含兩行數據的列表,該列表允許用戶單擊整個列表項中的任意位置以進行導航。 我目前有這個:

<mat-nav-list>
  <h3 matSubheader>Recent</h3>
  <mat-list-item *ngFor="let book of recentBooks$ | async">
      <a matLine routerLink="/book/{{book.id}}"><strong>{{book.title}}</strong></a>
      <p matLine>{{book.description}}</p>
  </mat-list-item>
</mat-nav-list>

這通常有效,但是單擊book.description的行不會導航。 我試過像這樣使用a外部:

<mat-nav-list>
  <h3 matSubheader>Recent</h3>
  <mat-list-item *ngFor="let book of recentBooks$ | async">
    <a routerLink="/book/{{book.id}}">
      <p matLine><strong>{{book.title}}</strong></p>
      <p matLine>{{book.description}}</p>
    </a>
  </mat-list-item>
</mat-nav-list>

但是,然后列表項不能正確顯示。 我嘗試將routerLink屬性添加到這兩行,並且可以,但是我認為這不是正確的方法。

有沒有一種更優雅的方式來實現我所缺少的功能?

嘗試在mat-list-item標簽中添加routerLink="/book/{{book.id}}"並刪除標簽

這可能有效

您可以嘗試以下代碼,它對我有用

<h3 matSubheader>Recent</h3>
<mat-list-item>
 <a [routerLink]="['/book',book.id]">
   <p matLine><strong>book</strong></p>
   <p matLine>This is a book</p>
 </a>
</mat-list-item>
</mat-nav-list>```

得到它的工作。 *ngFordiv ,然后將mat-list-itemrouterLink一起routerLink

<mat-nav-list>
  <h3 matSubheader>Recent</h3>
  <div *ngFor="let book of recentBooks$ | async">
    <a mat-list-item routerLink="/book/{{book.id}}">
      <p matLine><strong>{{book.title}}</strong></p>
      <p matLine>{{book.description}}</p>
    </a>
  </div>
</mat-nav-list>

我不知道是否沒有將mat-nav-list的直接子mat-list-item's作為mat-list-item's不利條件,但看起來還可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM