【问题标题】:How to pass a variable to toggle a specific panel inside an accordion in Angular 2如何在Angular 2中传递变量以切换手风琴内的特定面板
【发布时间】:2018-05-30 22:18:27
【问题描述】:

我有一个使用 ngFor 生成的 ngbpanels 列表。我需要根据一些逻辑扩展/取消扩展特定面板,但是我为面板 ID 传递的名称是一个变量。该代码无法识别面板 ID,因此不会切换它。如何格式化变量以便识别它。

在html文件中

<ngb-accordion #acc="ngbAccordion" >
<ngb-panel *ngFor="let panel of panels; let panelIndex=index" id="{{panelIndex}}">
....

在ts文件中:

@ViewChild('acc') panelsView;
....
this.panelsView.toggle(this.panelIndex);

问题在于,toggle 会采用这样的字符串:.toggle('panelId'),其中 panelId 是表示面板 id 的真实字符串。问题是我想传递一个变量。我试过 .toggle(" '+this.panelIndex+' ") ,但是没有用,有什么建议吗?

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    panelIndex: number转换成字符串并传递给toggle()方法:

    this.panelsView.toggle('' + this.panelIndex);
    

    http://plnkr.co/edit/6K83uv54zV0PFjrt0EO4?p=preview 上查看工作中的 plunkr

    来源:What's the best way to convert a number to a string in JavaScript?https://ng-bootstrap.github.io/#/components/accordion/api

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 2017-06-11
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多