【问题标题】:Angular repeat html for children using ng-templateAngular 使用 ng-template 为孩子重复 html
【发布时间】:2021-01-05 10:39:39
【问题描述】:

我认为这很简单,但由于某种原因我无法弄清楚。 我正在构建一个简单的子菜单。我已经创建了组件:

export class SubMenuComponent implements OnInit {
    @Input() links: MenuItem[];

    constructor() {}

    ngOnInit(): void {}
}

MenuItem 如下所示:

export class MenuItem {
    label: string;
    path: string;
    open: boolean;
    children?: MenuItem[];
}

而 Html 看起来像这样:

<ul class="app-sub-menu list-unstyled">
    <li *ngFor="let link of links" routerLinkActive="active"><a class="btn-link" [routerLink]="link.path"
            routerLinkActive="active" [routerLinkActiveOptions]="{exact: link.path === '/'}">{{ link.label }}</a>

        <span class="toggle" *ngIf="link.children?.length" (click)="link.open = !link.open">
            <mat-icon *ngIf="link.isActive || link.open">keyboard_arrow_down</mat-icon>
            <mat-icon *ngIf="!link.isActive && !link.open">keyboard_arrow_up</mat-icon>
        </span>

        <ul class="list-unstyled" [class.open]="link.open" *ngIf="link.children?.length">
            <li *ngFor="let link of link.children" routerLinkActive="active"><a class="btn-link"
                    routerLinkActive="active" [routerLink]="link.path" #link>{{ link.label }}</a>

                <span class="toggle" *ngIf="link.children?.length" (click)="link.open = !link.open">
                    <mat-icon *ngIf="link.isActive || link.open">keyboard_arrow_down</mat-icon>
                    <mat-icon *ngIf="!link.isActive && !link.open">keyboard_arrow_up</mat-icon>
                </span>

                <ul class="list-unstyled" [class.open]="link.open" *ngIf="link.children?.length">
                    <li *ngFor="let link of link.children" routerLinkActive="active"><a class="btn-link"
                            routerLinkActive="active" [routerLink]="link.path">{{ link.label }}</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

如您所见,当前的子菜单只下降了 3 个级别。我希望它是无限嵌套的。 所以我决定使用 ng-template,但我无法让它工作。 我认为这很简单:

<ul class="app-sub-menu list-unstyled">
    <ng-template *ngTemplateOutlet="link" *ngFor="let link of links"></ng-template>
</ul>

<ng-template #link>
    <li *ngFor="let link of links" routerLinkActive="active"><a class="btn-link" [routerLink]="link.path"
            routerLinkActive="active" [routerLinkActiveOptions]="{exact: link.path === '/'}">{{ link.label }}</a>

        <span class="toggle" *ngIf="link.children?.length" (click)="link.open = !link.open">
            <mat-icon *ngIf="link.isActive || link.open">keyboard_arrow_down</mat-icon>
            <mat-icon *ngIf="!link.isActive && !link.open">keyboard_arrow_up</mat-icon>
        </span>

        <ul class="list-unstyled" [class.open]="link.open" *ngIf="link.children?.length">
            <ng-template *ngTemplateOutlet="link" *ngFor="let link of link.children"></ng-template>
        </ul>
    </li>
</ng-template>

但是当我尝试使用它时,我得到一个错误:

一个元素上不能有多个模板绑定。只使用一个以 * 为前缀的属性

所以我把它改成了这样:

现在我得到一个新错误:

templateRef.createEmbeddedView 不是函数

有谁知道我可以做些什么来让它工作?


阅读:

https://medium.com/@nehaabrol87/the-power-of-angulars-ngtemplateoutlet-used-to-implement-a-nested-data-structure-11c741e6b48c

看来我的模板使用不正确,所以我将其更改为:

<ul class="app-sub-menu list-unstyled">
    <ng-container *ngTemplateOutlet="link; context: { $implicit: links }"></ng-container>
</ul>

<ng-template #link let-links>
    <li *ngFor="let link of links" routerLinkActive="active"><a class="btn-link" [routerLink]="link.path"
            routerLinkActive="active" [routerLinkActiveOptions]="{exact: link.path === '/'}">{{ link.label }}</a>

        <span class="toggle" *ngIf="link.children?.length" (click)="link.open = !link.open">
            <mat-icon *ngIf="link.isActive || link.open">keyboard_arrow_down</mat-icon>
            <mat-icon *ngIf="!link.isActive && !link.open">keyboard_arrow_up</mat-icon>
        </span>

        <ul class="list-unstyled" [class.open]="link.open" *ngIf="link.children?.length">
            <ng-container *ngTemplateOutlet="link; context: { $implicit: link.children }"></ng-container>
        </ul>
    </li>
</ng-template>

但我仍然收到错误:(

templateRef.createEmbeddedView 不是函数

【问题讨论】:

    标签: angular ng-template


    【解决方案1】:

    我想通了,这是因为我的模板 id 与实际模型相同。所以我把它改成了这样:

    <ul class="app-sub-menu list-unstyled">
        <ng-template #nestedList let-links>
            <li *ngFor="let link of links" routerLinkActive="active"><a class="btn-link" [routerLink]="link.path"
                    routerLinkActive="active" [routerLinkActiveOptions]="{exact: link.path === '/'}">{{ link.label }}</a>
    
                <span class="toggle" *ngIf="link.children?.length" (click)="link.open = !link.open">
                    <mat-icon *ngIf="link.isActive || link.open">keyboard_arrow_down</mat-icon>
                    <mat-icon *ngIf="!link.isActive && !link.open">keyboard_arrow_up</mat-icon>
                </span>
    
                <ul class="list-unstyled" [class.open]="link.open" *ngIf="link.children?.length">
                    <ng-container *ngTemplateOutlet="nestedList; context: { $implicit: link.children }"></ng-container>
                </ul>
            </li>
        </ng-template>
        <ng-container *ngTemplateOutlet="nestedList; context: { $implicit: links }"></ng-container>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 2018-07-03
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多