Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 527
El Dude Avatar asked Dec 14 '16 19:12

El Dude


1 Answers

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...)
}
like image 175
Onno Avatar answered Nov 12 '22 01:11

Onno