angular - 带数据循环的折叠区域

标签 angular ng-bootstrap

我有一个 SPA(单页应用程序),其中有一个表格可以扩展详细信息。

这是代码:

<div class="row mt-4" *ngFor="let row of rows">
    <div class="col-12">
        <button class="btn btn-link" (click)="isCollapsed = !isCollapsed"
          [attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample">
            <i class="fa" aria-hidden="true"></i>
            Details
        </button>
        <div [ngbCollapse]="isCollapsed">
            {{ row.detail}}
        </div>
    </div>
</div>

我无法让它仅展开单击列的详细信息

最佳答案

我是这样解决的,我想这比实际更改数据本身更方便。

在组件的 HTML 部分中,获取 ngFor 循环的索引,并在事件绑定(bind)和 ngbCollapse 指令中使用此索引变量。 要实现此功能,您的 component.ts 文件中需要一个 bool 属性数组。

组件 HTML

<div *ngFor="let row of rows; let i = index">
    <button type="button" (click)="isCollapsed[i] = !isCollapsed[i]">
        Button Text
    </button>
    <div [ngbCollapse]="isCollapsed[i]">
        .. some content ..
    </div>
</div>

组件 TypeScript

@Component({
  selector: 'xxx',
  templateUrl: './xxx.component.html',
  styleUrls: [ './xxx.component.css']
})
export class xxx{

  public isCollapsed: boolean[] = [];

}

关于angular - 带数据循环的折叠区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703931/

相关文章:

javascript - 如何使用 Html iframe 在 Angular 内运行 ionic 应用程序

angular - 如何使用 Angular ( Angular 2 或 4)路由更改页面标题?

angular - 返回 Observable 并以 Angular 订阅

javascript - Angular 6 : error TS2339: Property 'value' does not exist on type 'HTMLElement'

angular - 仅从 ng-bootstrap 安装 datepicker

angular - NgbModal.open 在 Angular 4 中不起作用

html - ngtsc(2345) - 'Event' 类型的参数不能分配给 'SortEvent' 类型的参数

angularjs - Ng-bootstrap 导入错误 Angular 2

javascript - Angular/HTML - 将显示完整的 JSON 但不能仅显示单个属性

angular - 使用 ReactiveForms 和 NgbModal 时的 ExpressionChangedAfterItHasBeenCheckedError