How to toggle Angular material expansion panel programmatically

前端 未结 6 1111
半阙折子戏
半阙折子戏 2021-02-12 16:20

I just started working on an Angular 4 project with material design.

I am currently working with the expansion component, the API states that a disabled expansion pa

6条回答
  •  清歌不尽
    2021-02-12 17:01

    Suppose you are looking for the way to trigger open() method from the component on button click, this is what I've been able to get from the material API documentation.

    import { MatAccordion, MatExpansionPanel } from '@angular/material/expansion';
    
    export class MyComponent implements OnInit {
      @ViewChild(MatExpansionPanel) pannel?: MatExpansionPanel; 
      @ViewChild(MatAccordion) accordion?: MatAccordion;
    
    constructor(){}
    
      closePannel() { // for expansion panel only
        if(!this.pannel) { return }
        this.pannel.close();
      }
    
      closeAll() { // for all panels in accordion
        if(!this.accordion) { return }
        this.accordion.closeAll();
      }
    }
    

提交回复
热议问题