0

I have animated ngx-bootstrap-accordion but if I click fastly enough the label is not consist with state of accordion. I want the label to say hide if there is something to hide, not when the content is visible.

Update example: https://stackblitz.com/edit/ngx-bootstrap-accordion-asasasa2-zb5ki6

See blitz here: https://stackblitz.com/edit/ngx-bootstrap-accordion-asasasa2

app.component.ts

import { ViewChild } from '@angular/core';
import { AccordionPanelComponent } from 'ngx-bootstrap/accordion';
import {AfterViewInit, Component, Inject, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {ViewportScroller} from '@angular/common';
import {Router} from '@angular/router';
import { StorageService, SESSION_STORAGE} from 'ngx-webstorage-service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  sections = [
    {
      code: 'a',
      visible: true,
      subsections: null
    },
    {
      code: 'p',
      visible: true,
      subsections: null
    }
  ];
}

app.component.html

<div class="toolbar">
    <h2>Toolbar</h2>
</div>

<div class="content">
    <accordion [isAnimated]="true">
        <accordion-group *ngFor="let subsection of sections" [isOpen]="subsection.visible" class="app-accordion-group">

            <div class="app-subsection-accordion-header" accordion-heading >

                <button *ngIf="subsection.visible"  (click)="subsection.visible = !subsection.visible">
               Open panel 2
             </button>

                <button *ngIf="!subsection.visible" (click)="subsection.visible = !subsection.visible">
              Hide panel 2
            </button>

                <div class="app-header-title">
                    text
                </div>
            </div>
            <div class="app-document app-desktopVisible">
                doc
            </div>
        </accordion-group>
    </accordion>
</div>
4

1 回答 1

1

Since you mutating visible property on template the reference of object is not changing so accordion component change detection is not get triggered.

Try this:

component.html

    <button *ngIf="subsection.visible"  (click)="toggle(i)">
           Open panel
     </button>

component.ts

toggle(index){
    this.sections[index] = {
      ... this.sections[index],visible: ! this.sections[index].visible
 }
}

Forked Example

于 2020-03-05T15:55:50.173 回答