我正在尝试在Angular中构建一个向导组件。为了制作进度条和隐藏/显示步骤,我希望能够通过计算"步骤"的数量来计算向导中的步数。里面的模板。我刚刚了解了ContentChildren
的概念,它给了我elementRef
个对象,但我不知道如何从那里开始:
wizard.component.ts
import { ... } from '@angular/core
export class WizardComponent implements AfterViewInit, OnInit {
@ContentChildren('step') steps: QueryList<any>;
}
ngAfterViewInit() {
this.steps.forEach(step => console.log(step));
}
wizard.component.html
<ng-content></ng-content>
app.component.html
<app-wizard>
<div #step> First Step Screen </div>
<div #step> Second Step Screen </div>
</app-wizard>