我会创建一个指令,如:
@Directive({
selector: '[stepper-section]'
})
export class StepperSectionDirective {}
然后stepper-section
为每个部分添加属性:
<stepper>
<section stepper-section>content here<section>
<section stepper-section>content here<section>
<section stepper-section>content here<section>
</stepper>
最后使用@ContentChildren
装饰器查询所有部分:
@ContentChildren(StepperSectionDirective) sections: QueryList<StepperSectionDirective>;
Ng 运行示例
如果你想遍历内容并动态渲染它,你可以用StepperComponent包装你的孩子ng-template
并使用指令来渲染它们:ngTemplateOutlet
html
<app-stepper>
<ng-template stepper-section>Content 1</ng-template>
<ng-template stepper-section>Content 2</ng-template>
<ng-template stepper-section>Content 3</ng-template>
</app-stepper>
stepper-section.directive.ts
@Directive({
selector: '[stepper-section]'
})
export class StepperSectionDirective {
hidden = false;
constructor(public templateRef: TemplateRef<any>) {}
}
stepper.component.ts
@ContentChildren(StepperSectionDirective) sectionDirs: QueryList<StepperSectionDirective>;
stepper.component.html
<button *ngFor="let section of sections; index as i;"
[class.enabled]="activeSection === i" (click)="goTo(i)">
Step {{ i + 1 }}
</button>
<div class="content">
<ng-container *ngFor="let section of sections">
<ng-template *ngIf="!section.hidden"
[ngTemplateOutlet]="section.templateRef"></ng-template>
</ng-container>
</div>
Ng 运行示例
这两种方法的区别在于,在第一种情况下,所有子内容都被渲染,我们只操作display: none
我们想要隐藏的那些步骤。
在第二种方法中,我们可以更好地控制要渲染的内容,并且在特定时间只渲染一个步骤。