gpt4 book ai didi

angular - 如何在 Angular 2.0 中以编程方式触发 PrimeNG Accordion Click?

转载 作者:太空狗 更新时间:2023-10-29 18:07:03 27 4
gpt4 key购买 nike

我有一个 Accordion ,如下所示。

当链接被点击时,我需要触发所有 p-accordianTab 元素的点击事件。

这怎么可能?

<a (click)="openCloseAll()" >{{openCloseAllText}} all</a>
<p-accordion [multiple]="true">
<div class="row" *ngFor="let category of result.Categories">

<p-accordionTab #accordianTab header="{{category.Name}}">

</p-accordionTab>

</div>
</p-accordion>

我尝试将此“#accordionTab”添加到元素并从 TypeScript 访问它但不起作用:

@ViewChild('accordionTab') accordionTab: ElementRef;
openCloseAllText: string = "Open";
openCloseAll() {
// get all accordions and click them
this.openCloseAllText = this.openCloseAllText === "Open" ? "Close" : "Open";
this.accordionTab.nativeElement.click();
}

TypeError: Cannot read property 'nativeElement' of undefined

最佳答案

为什么不直接使用 Accordion 本身的制表符属性呢?

<p-accordion #accordion>
<p-accordionTab header="Header Content">
Body Content
</p-accordionTab>
</p-accordion>

@ViewChild('accordion') accordion: Accordion;

closeAllAccordionTabs() {
if(!isNullOrUndefined(this.accordion.tabs)){
for(let tab of this.accordion.tabs) {
if(tab.selected) tab.selected = false;
}
}
}

openAllAccordionTabs() {
if(!isNullOrUndefined(this.accordion.tabs)){
for(let tab of this.accordion.tabs) {
if(!tab.selected) tab.selected = true;
}
}
}

关于angular - 如何在 Angular 2.0 中以编程方式触发 PrimeNG Accordion Click?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42901922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com