Search code examples
angularionic2floating-action-button

Ionic2 close fab menu when button pressed


How can I close the open FAB menu when a button in the menu was pressed?

Here's my button.

<ion-fab bottom center >
<button ion-fab><b>Hello</b></button>
<ion-fab-list side="top">
<button ion-fab (click)="refresh()"><ion-icon name="refresh"></ion-icon></button>

What would I have to add to refresh to make the whole FAB close? Can I somehow reference the html object from within the code? Similar to the way iOS handles it's outlets?


Solution

  • Just add #fab to the ion-fab tag:

    <ion-fab bottom center #fab>
        <button ion-fab (click)="refresh(fab)"> 
            <ion-icon name="refresh"></ion-icon>       
        </button>
    </ion-fab>
    

    and use the FabContainer method close() to close the FAB menu (if opened) at the beginning of any function called by your FAB buttons, e.g.:

    import { FabContainer } from 'ionic-angular';
    
    // remember to pass the fab from client side
    refresh(fab?: FabContainer): void {
        if (fab !== undefined) {
          fab.close();
        }
        (other function stuff...)
    }