如何在ng手风琴面板的显示屏上附加路线?

德文·菲利普斯(Deven Phillips)

我使用*ngFor循环定义了ng手风琴,我想调整位置并将路线绑定到特定面板的视图。理想情况下,当客户单击以展开面板时,位置将在浏览器中更新,而新的历史记录项将存储在浏览器中。另外,如果客户输入的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>

映射的路由可能是:

/faq/ABCD
/faq/EFGH
/faq/IJKL

切换特定面板将更新位置/ 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>

组件(在此示例中,我模拟了数据):

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);
  }

}

我为路由参数添加了一个预订,以便我们可以在更改路由时重置selectedFaqId。

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

我通过绑定到panelChange事件来响应手风琴面板的手动扩展。在这种情况下,我们会将路线设置为所选的常见问题ID。这会将网址导航到selectedFaqId。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章