From 924bcc28ae77a8681163f985be27c160c14fb75c Mon Sep 17 00:00:00 2001 From: nuonuoge <932273325@qq.com> Date: Mon, 25 Mar 2019 10:42:36 +0800 Subject: [PATCH] fix(module: accordion): fix accordion support async load accordionPanel (#355) --- .../accordion/accordion.component.spec.ts | 11 +++++++++-- components/accordion/accordion.component.ts | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/components/accordion/accordion.component.spec.ts b/components/accordion/accordion.component.spec.ts index aaa2b6dd..330887bf 100644 --- a/components/accordion/accordion.component.spec.ts +++ b/components/accordion/accordion.component.spec.ts @@ -1,4 +1,4 @@ -import { Component, ViewChild } from '@angular/core'; +import { Component, ViewChild, OnInit } from '@angular/core'; import { By } from '@angular/platform-browser'; import { async, ComponentFixture, fakeAsync, tick, TestBed } from '@angular/core/testing'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @@ -112,7 +112,7 @@ describe('AccordionComponent', () => { ` }) -export class TestAccordionComponent { +export class TestAccordionComponent implements OnInit { defaultActiveKey = '0'; accordions: Array = [{ title: 'Title 1', child: ['content 1', 'content 1', 'content 1'], inactive: false }, { title: 'Title 2', child: ['content 2', 'content 2', 'content 2'] }]; @@ -125,4 +125,11 @@ export class TestAccordionComponent { onChange(event) { console.log(event); } + + ngOnInit() { + setTimeout(() => { + this.accordions = [{ title: 'Title 1', child: ['content 1', 'content 1', 'content 1'], inactive: false }, + { title: 'Title 2', child: ['content 2', 'content 2', 'content 2'] }]; + }, 0); + } } diff --git a/components/accordion/accordion.component.ts b/components/accordion/accordion.component.ts index e6139ee0..fe8a5953 100644 --- a/components/accordion/accordion.component.ts +++ b/components/accordion/accordion.component.ts @@ -25,6 +25,8 @@ import { Subscription } from 'rxjs'; export class AccordionComponent implements AfterContentInit, OnDestroy, OnChanges { private _oldGroups: AccordionGroupComponent[]; private _subscription: Subscription; + private groupsSubscription: Subscription; + private isFirstChange: boolean = true; @ContentChildren(forwardRef(() => AccordionGroupComponent)) groups: QueryList; @@ -95,6 +97,7 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange if (index === parseInt(key, 0)) { setTimeout(() => { group.isOpened = true; + group.openOnInitialization(); }, 0); } }); @@ -104,6 +107,7 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange if (index === parseInt(this.defaultActiveKey, 0)) { setTimeout(() => { group.isOpened = true; + group.openOnInitialization(); }, 0); } }); @@ -129,12 +133,24 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange } ngAfterContentInit() { - this.init(); + if (this.groups && this.groups.length > 0) { + this.init(); + } else { + this.groupsSubscription = this.groups.changes.subscribe(group => { + if (this.isFirstChange) { + this.init(); + } + this.isFirstChange = false; + }); + } } ngOnDestroy() { if (this._subscription) { this._subscription.unsubscribe(); } + if (this.groupsSubscription) { + this.groupsSubscription.unsubscribe(); + } } }