File

src/app/accordion/accordion.component.ts

Description

The accordion component.

Implements

AfterContentInit

Metadata

selector app-accordion
styleUrls accordion.component.scss
templateUrl ./accordion.component.html

Index

Properties
Methods

Constructor

constructor(cdRef: ChangeDetectorRef)
Parameters :
Name Type Optional
cdRef ChangeDetectorRef no

Methods

ngAfterContentInit
ngAfterContentInit()

The Accordion component lifecycle hook that is called after a directive's content has been fully initialized.

Returns : void

void

Public openPanel
openPanel(panel: PanelComponent)

Set current panel to open state.

Parameters :
Name Type Optional
panel PanelComponent no
Returns : void

void

Properties

panels
panels: QueryList<PanelComponent>
Type : QueryList<PanelComponent>
Decorators : ContentChildren

The accordion query list panel.

import {Component, ContentChildren, OnInit, QueryList, AfterContentInit, ChangeDetectorRef} from '@angular/core';
import {PanelComponent} from '../panel/panel.component';

/**
 * The accordion component.
 */
@Component({
  selector: 'app-accordion',
  templateUrl: './accordion.component.html',
  styleUrls: ['./accordion.component.scss']
})

/**
 * @class AccordionComponent
 * @implements AfterContenInit
 */
export class AccordionComponent implements AfterContentInit {

  /**
   * The accordion query list panel.
   * @typedef QueryList
   */
  @ContentChildren(PanelComponent) panels: QueryList<PanelComponent>;

  /**
   * The Accordion component lifecycle hook
   * that is called after a directive's content has been
   * fully initialized.
   * @returns void
   */
  ngAfterContentInit(): void {
    this.cdRef.detectChanges();
    this.panels.toArray().forEach((panel: PanelComponent) => {
      panel.toggle.subscribe(() =>  setTimeout(() => {
        this.openPanel(panel);
      }));
    });

  /**
   * Set first panel open as default.
   */
    setTimeout(() => {
      this.openPanel(this.panels.toArray()[0]);
    });
  }

  /**
   * Set current panel to open state.
   * @param {panel} PanelComponent - current panel
   * @returns void
   */
  public openPanel(panel: PanelComponent): void {
    this.panels.toArray().forEach(p => p.opened = false);
    panel.opened = true;
  }

  /**
   * @constructor
   * @param {cdRef} ChangeDetectorRef - Change detector rerference
   */
  constructor(private cdRef: ChangeDetectorRef) { }

}
<ng-content></ng-content>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""