html 如何在Angular 中的for循环中打开扩展面板

m1m5dgzv  于 2023-06-04  发布在  Angular
关注(0)|答案(1)|浏览(270)

从下面的代码中,我有一个面板出现在for循环中,我想默认打开第一个索引paenl,我在下面发布我的实现,

<div *ngFor="let facility of slots; let i = index" class="facility-box">
        <div class="facility">
            <div >
                <button igxButton="outlined" class="btn" (click)="onClickViewSlots(slotExpPanel, facility)">
                    VIEW TIMES ({{facility?.slots[0].times.length}}) 
                </button>
            </div>
        </div>
        <div>
            <igx-expansion-panel #slotExpPanel>
                <igx-expansion-panel-body>
                    <div>
                           <div class="date date-appointment" *ngIf="facility?.slots[0]?.times.length > 0">
                                {{facility?.slots[0].date | date: 'EEE, MMM dd'}} :
                            </div>
                    </div>
                </igx-expansion-panel-body>
            </igx-expansion-panel>
        </div>
    </div>

组件:

`export class TestPanel implements OnInit {

   @ViewChild('slotExpPanel', { read: IgxExpansionPanelComponent, static: true })
   public slotExpPanel!: IgxExpansionPanelComponent;

   constructor() {
      this.slotExpPanel.open();

   }
 }


错误:

Cannot find open() of undefined

有人可以建议我在这里帮助。谢谢
我试图在for循环中打开第一个索引面板

f87krz0w

f87krz0w1#

使用OnInit而不是构造函数。当OnInit运行时,您的组件已经创建。在构造函数运行期间可能尚未分配ViewChild。这里有更多的细节:Difference between Constructor and ngOnInit

相关问题