Angular 如何将布线连接到ng手风琴面板的显示器?

Angular 如何将布线连接到ng手风琴面板的显示器?,angular,ng-bootstrap,angular-component-router,Angular,Ng Bootstrap,Angular Component Router,我有一个使用*ngFor循环定义的ng手风琴,我想调整位置并将路线绑定到特定面板的视图。理想情况下,当客户端单击展开面板时,位置将在浏览器中更新,并且新的历史记录项将存储在浏览器中。此外,如果客户机输入一个URL,该URL将对应于正在显示的特定手风琴项目,我希望确保反映适当的状态 例如: <ngb-accordion closeOthers="true"> <ngb-panel *ngFor="let faq of faqService.getItems()" id="{{

我有一个使用
*ngFor
循环定义的ng手风琴,我想调整位置并将路线绑定到特定面板的视图。理想情况下,当客户端单击展开面板时,位置将在浏览器中更新,并且新的历史记录项将存储在浏览器中。此外,如果客户机输入一个URL,该URL将对应于正在显示的特定手风琴项目,我希望确保反映适当的状态

例如:

<ngb-accordion closeOthers="true">
  <ngb-panel *ngFor="let faq of faqService.getItems()" id="{{faq.id}}" title="{{faq.title}}">
    <ng-template ngbPanelContent>
      {{faq.body}}
    </ng-template>
  </ngb-panel>
</ngb-accordion>

切换特定面板将更新位置/ActiveRoute,粘贴映射到特定面板的URL将导致该面板展开。有关于如何连接的建议吗?

您的应用程序路由需要这样配置,我们需要faqId作为路由的参数:

export const appRoutes = [
  {
    path: 'faq/:faqId',
    component: FaqComponent
  }
];
并导入到您的模块中,如下所示:

imports: [RouterModule.forRoot(appRoutes)]
在标记中:

<ngb-accordion closeOthers="true" [activeIds]="selectedFaqId" (panelChange)="onPanelChange($event)">
  <ngb-panel *ngFor="let faq of faqs" id="{{faq.id}}" title="{{faq.title}}"  >
    <ng-template ngbPanelContent>
      {{faq.body}}
    </ng-template>
  </ngb-panel>
</ngb-accordion>
我已经为路由参数添加了订阅,这样我们可以在路由更改时重置selectedFaqId

我将selectedFaqId绑定到手风琴的SelectedId属性。这将展开所选Id的面板


我通过绑定到panelChange事件来响应手风琴面板的手动扩展。在这种情况下,我们将路由设置为所选的Faq Id。这将导航url到所选的Faq Id。

太棒了,谢谢!这看起来几乎可以完美地工作。是否有其他定义事件的文档,或者我只需要查找源代码就可以了?很高兴它对您有用。有一些简单的文档。我使用它和它们来找出要使用的事件和属性。
<ngb-accordion closeOthers="true" [activeIds]="selectedFaqId" (panelChange)="onPanelChange($event)">
  <ngb-panel *ngFor="let faq of faqs" id="{{faq.id}}" title="{{faq.title}}"  >
    <ng-template ngbPanelContent>
      {{faq.body}}
    </ng-template>
  </ngb-panel>
</ngb-accordion>
import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { NgbPanelChangeEvent } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-faq',
  templateUrl: './faq.component.html'
})
export class FaqComponent {

  selectedFaqId = '';
  faqs = [
    {
      id: 'a',
      title: 'faq 1 title',
      body: 'faq 1 body'
    },
    {
      id: 'b',
      title: 'faq 2 title',
      body: 'faq 2 body'
    }
  ];

  constructor(private route: ActivatedRoute, private router: Router) {
    route.params.subscribe(x => {
      this.selectedFaqId = x.faqId;
      console.log(this.selectedFaqId);
    })
  }

  onPanelChange(event: NgbPanelChangeEvent) {
    this.router.navigateByUrl(`faq/${event.panelId}`);
    console.log(event);
  }

}