【问题标题】:Accordion in ngFor AngularngFor Angular 中的手风琴
【发布时间】:2020-09-11 09:34:05
【问题描述】:

我有一个 ngFor,对于每个项目我都有一个按钮来显示一些额外的细节(我正在使用 bootstrap 手风琴)。 我的问题是我只想打开有关当前项目的手风琴(不是全部)。

我将向您展示我对索引的尝试,但它不起作用。

那是我的 ngFor:

<ul *ngFor="let star of stars; index as i;">
...
</ul

这是我的按钮:

<button type="button" data-toggle="collapse" attr.[data-target]="'collapse' + i" aria-expanded="false"></button>

这是手风琴:

<div class="collapse" [id]="'collapse' + i">
<div class="card card-body">
...

这是我的错误:

ERROR DOMException: Failed to execute 'setAttribute' on 'Element': 'attr.[data-target]' is not a valid attribute name.

你能帮帮我吗?谢谢!

【问题讨论】:

    标签: angular bootstrap-4 accordion ngfor


    【解决方案1】:

    试试这个:

    <div class="collapse" [attr.id]="'collapse' + i">
    
    <button type="button" data-toggle="collapse" [attr.data-target]="'#collapse' + i" aria-expanded="false"></button>
    

    如果这不是你想要的,那么有一个 stackblitz 会很好:-)

    见:Attribute, class, and style bindings

    【讨论】:

    • 我已经用错误编辑了我的问题。问题在于数据目标
    • [attr.data-target]="'#collapse' + i"
    • 我认为缺少一个#
    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2020-10-01
    • 2013-12-19
    • 1970-01-01
    相关资源
    最近更新 更多