Angular Material List not rendering list properly when the list item is a component

Objective

I am attempting to encapsulate the formatting logic (not routing logic) into a component.
I am using the Angular Material’s MatList component.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><mat-nav-list>
<!-- The below <a> has some common routing and styling logics -->
<a
mat-list-item
[class.active]="isActive"
routerLinkActive="active2"
[activated]="isActive"
[routerLink]="link"
>
<!-- Control formatting -->
@if(singleLine) {
<app-list-single-line-format [item]="item" />
} @else {
<app-list-multi-line-format [item]="item" />
}
</a>
</mat-nav-list>
</code>
<code><mat-nav-list> <!-- The below <a> has some common routing and styling logics --> <a mat-list-item [class.active]="isActive" routerLinkActive="active2" [activated]="isActive" [routerLink]="link" > <!-- Control formatting --> @if(singleLine) { <app-list-single-line-format [item]="item" /> } @else { <app-list-multi-line-format [item]="item" /> } </a> </mat-nav-list> </code>
<mat-nav-list>
  <!-- The below <a> has some common routing and styling logics  -->
  <a
    mat-list-item
    [class.active]="isActive"
    routerLinkActive="active2"
    [activated]="isActive"
    [routerLink]="link"
  >
    <!-- Control formatting -->
    @if(singleLine) {
    <app-list-single-line-format [item]="item" />
    } @else {
    <app-list-multi-line-format [item]="item" />
    }
  </a>
</mat-nav-list>

It is almost working. But the styling is broken when used as a component.

Minimal reproduction

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><mat-nav-list role="list">
<!-- working -->
<a mat-list-item role="listitem">
<mat-icon matListItemIcon>folder</mat-icon>
<div matListItemTitle>hello</div>
<div matListItemLine>world</div>
</a>
<!-- Broken styles -->
<a mat-list-item role="listitem">
<app-list-item />
</a>
</mat-nav-list>
</code>
<code><mat-nav-list role="list"> <!-- working --> <a mat-list-item role="listitem"> <mat-icon matListItemIcon>folder</mat-icon> <div matListItemTitle>hello</div> <div matListItemLine>world</div> </a> <!-- Broken styles --> <a mat-list-item role="listitem"> <app-list-item /> </a> </mat-nav-list> </code>
<mat-nav-list role="list">
  <!-- working -->
  <a mat-list-item role="listitem">
    <mat-icon matListItemIcon>folder</mat-icon>
    <div matListItemTitle>hello</div>
    <div matListItemLine>world</div>
  </a>

  <!-- Broken styles -->
  <a mat-list-item role="listitem">
    <app-list-item />
  </a>
</mat-nav-list>

Styling of the second item is broken. I don’t want to fix this by hacky css. Is there a proper way? Stackblitz

Excerpt:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Component({
selector: "app-list-item",
template: `
<mat-icon matListItemIcon>folder</mat-icon>
<div matListItemTitle>hello</div>
<div matListItemLine>world</div>
`,
standalone: true,
imports: [MatListModule, MatIconModule, RouterLink],
})
export class ListItem {}
@Component({
selector: "list-overview-example",
template: `
<mat-nav-list role="list">
<!-- working -->
<a mat-list-item role="listitem">
<mat-icon matListItemIcon>folder</mat-icon>
<div matListItemTitle>hello</div>
<div matListItemLine>world</div>
</a>
<!-- Broken styles -->
<a mat-list-item role="listitem">
<app-list-item />
</a>
</mat-nav-list>
`,
standalone: true,
imports: [MatListModule, MatIconModule, RouterLink, ListItem],
})
export class ListOverviewExample {}
</code>
<code>@Component({ selector: "app-list-item", template: ` <mat-icon matListItemIcon>folder</mat-icon> <div matListItemTitle>hello</div> <div matListItemLine>world</div> `, standalone: true, imports: [MatListModule, MatIconModule, RouterLink], }) export class ListItem {} @Component({ selector: "list-overview-example", template: ` <mat-nav-list role="list"> <!-- working --> <a mat-list-item role="listitem"> <mat-icon matListItemIcon>folder</mat-icon> <div matListItemTitle>hello</div> <div matListItemLine>world</div> </a> <!-- Broken styles --> <a mat-list-item role="listitem"> <app-list-item /> </a> </mat-nav-list> `, standalone: true, imports: [MatListModule, MatIconModule, RouterLink, ListItem], }) export class ListOverviewExample {} </code>
@Component({
  selector: "app-list-item",
  template: `
    <mat-icon matListItemIcon>folder</mat-icon>
    <div matListItemTitle>hello</div>
    <div matListItemLine>world</div>
  `,
  standalone: true,
  imports: [MatListModule, MatIconModule, RouterLink],
})
export class ListItem {}

@Component({
  selector: "list-overview-example",
  template: `
    <mat-nav-list role="list">
      <!-- working -->
      <a mat-list-item role="listitem">
        <mat-icon matListItemIcon>folder</mat-icon>
        <div matListItemTitle>hello</div>
        <div matListItemLine>world</div>
      </a>

      <!-- Broken styles -->
      <a mat-list-item role="listitem">
        <app-list-item />
      </a>
    </mat-nav-list>
  `,
  standalone: true,
  imports: [MatListModule, MatIconModule, RouterLink, ListItem],
})
export class ListOverviewExample {}

Here is the demo: https://stackblitz.com/edit/3ruxqq?file=src%2Fexample%2Flist-overview-example.html

2

Unfortunately, the way that you wrap those elements into a separate component is not working when applying it to the mat-list-item element.

mat-list-item has its own structure:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><ng-content select="[matListItemAvatar],[matListItemIcon]"></ng-content>
<span class="mdc-list-item__content">
<ng-content select="[matListItemTitle]"></ng-content>
<ng-content select="[matListItemLine]"></ng-content>
<span #unscopedContent class="mat-mdc-list-item-unscoped-content"
(cdkObserveContent)="_updateItemLines(true)">
<ng-content></ng-content>
</span>
</span>
...
</code>
<code><ng-content select="[matListItemAvatar],[matListItemIcon]"></ng-content> <span class="mdc-list-item__content"> <ng-content select="[matListItemTitle]"></ng-content> <ng-content select="[matListItemLine]"></ng-content> <span #unscopedContent class="mat-mdc-list-item-unscoped-content" (cdkObserveContent)="_updateItemLines(true)"> <ng-content></ng-content> </span> </span> ... </code>
<ng-content select="[matListItemAvatar],[matListItemIcon]"></ng-content>

<span class="mdc-list-item__content">
  <ng-content select="[matListItemTitle]"></ng-content>
  <ng-content select="[matListItemLine]"></ng-content>
  <span #unscopedContent class="mat-mdc-list-item-unscoped-content"
        (cdkObserveContent)="_updateItemLines(true)">
    <ng-content></ng-content>
  </span>
</span>

...

With the additional component, this results in those elements located in the nested HTML element (<app-list-item>), and MatList unable to query for <mat-icon>, matListItemTitle and matListItemLine elements correctly.

Instead, all the elements will be rendered in the <ng-content> part resulting in the styles not working well.

Thus, would suggest you don’t need the additional component if that component is just for display and without any additional purpose. Just place the HTML structure within the mat-list-item.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><mat-nav-list>
<!-- The below <a> has some common routing and styling logics -->
<a
mat-list-item
[class.active]="isActive"
routerLinkActive="active2"
[activated]="isActive"
[routerLink]="link"
>
<!-- Control formatting -->
@if(singleLine) {
<mat-icon matListItemIcon>{{ /* item.icon */ }}</mat-icon>
<div matListItemTitle>{{ /* item.title */ }</div>
<div matListItemLine>{{ /* item.line */ }</div>
} @else {
<!-- HTML Structure from <app-list-multi-line-format> -->
}
</a>
</mat-nav-list>
</code>
<code><mat-nav-list> <!-- The below <a> has some common routing and styling logics --> <a mat-list-item [class.active]="isActive" routerLinkActive="active2" [activated]="isActive" [routerLink]="link" > <!-- Control formatting --> @if(singleLine) { <mat-icon matListItemIcon>{{ /* item.icon */ }}</mat-icon> <div matListItemTitle>{{ /* item.title */ }</div> <div matListItemLine>{{ /* item.line */ }</div> } @else { <!-- HTML Structure from <app-list-multi-line-format> --> } </a> </mat-nav-list> </code>
<mat-nav-list>
  <!-- The below <a> has some common routing and styling logics  -->
  <a
    mat-list-item
    [class.active]="isActive"
    routerLinkActive="active2"
    [activated]="isActive"
    [routerLink]="link"
  >
    <!-- Control formatting -->
    @if(singleLine) {
      <mat-icon matListItemIcon>{{ /* item.icon */ }}</mat-icon>
      <div matListItemTitle>{{ /* item.title */ }</div>
      <div matListItemLine>{{ /* item.line */ }</div>
    } @else {
      <!-- HTML Structure from <app-list-multi-line-format> -->
    }
  </a>
</mat-nav-list>

Otherwise, you need to move the <a mat-list-item> to the ListItem component. This requires you to add the @Input binding in the component to receive the data such as isActive, link and etc.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật