Search code examples
angularjspopoveronsen-ui

Hide popover in onsen ui


So before someone jumps on me and shout that there is already a solution for this, I'm sorry but it doesn't work for me. I had a look and tried both options proposed in the questions bellow, with no success. How to hide popover in onsen ui

I don't really understand why as my code is almost identical to the examples. I use a sliding menu on the page but that's pretty much it.

Here is my index.html

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script src="components/loader.js"></script>

<link rel="stylesheet" href="components/loader.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/sliding_menu.css">
<script>

    var app = ons.bootstrap('propertyDeal', ['onsen']);
    app.controller('AppController', function($scope, myService) {
        ons.createPopover('popover.html',{parentScope: $scope}).then(function(popover) {
            $scope.popover = popover;
            myService.setPopover($scope.popover);
        });
    })
    app.controller('HydePController', function($scope, myService) {
        $scope.destroyPopover = function() {
            $scope.popover = myService.getPopover();    
            $scope.popover.hide();
        }
    });

    app.service("myService", function(){
        var sharedPopover

        var setPopover = function(pop){
            sharedPopover = pop;
        };

        var getPopover = function(){
            return sharedPopover;
        };

        return {
            setPopover: setPopover,
            getPopover: getPopover,
        };
    });
</script>
</head>
<body>
<ons-sliding-menu main-page="initial.html" menu-page="menu.html" max-slide-distance="260px" type="overlay" var="menu" side="left" close-on-tap>
</ons-sliding-menu>
<ons-template id="popover.html">
    <ons-popover direction="down" cancelable>
            <ons-list ng-controller="HydePController">
                <ons-list-item modifier="tappable" ng-click="menu.setMainPage('save.html', {closeMenu: true}); popover.hide()">
                    <ons-icon icon="fa-cloud"></ons-icon>
                    Save property
                </ons-list-item>
                <ons-list-item modifier="tappable" ng-click="hydepopover()">
                    <ons-icon icon="fa-home"></ons-icon>
                    View portfolio
                </ons-list-item>
            </ons-list>
    </ons-popover>

</ons-template>

</body>

And initial.html

<ons-navigator>
<ons-page>
    <ons-toolbar>
      <div class="left">
        <ons-toolbar-button ng-click="menu.toggleMenu()">
          <ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
        </ons-toolbar-button>
        <ons-icon class="icon" icon="ion-social-usd"></ons-icon>&nbsp;Initial investment
      </div>
      <div class="right">
        <ons-toolbar-button ng-click="menu.toggleMenu()">
          <ons-icon icon="ion-android-share" fixed-width="false"></ons-icon>
        </ons-toolbar-button>
            <ons-toolbar-button ng-controller="AppController" id="android-more" ng-click="popover.show($event);">
              <ons-icon icon="ion-android-more" fixed-width="false"></ons-icon>
            </ons-toolbar-button>
      </div>
    </ons-toolbar>

    <div style="text-align: center">
        <h2>Initial investment</h2>

        <ul class="list">
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" placeholder="Purchase price">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" placeholder="Market value">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" placeholder="Stamp duty">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" placeholder="Sourcing fee">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" placeholder="Legal fee">
            </li>
            <li class="list__item">
                <input type="text" class="text-input text-input--transparent" style="width:100%; margin-top:4px;" placeholder="Other">
            </li>
        </ul>
    </div>

        </ul>
    </div>

</ons-page>
</ons-navigator>

So as said above, I tried both solutions and no luck. I don't understand why. I also have the sliding menu that doesn't close when clicking outside of the menu. Could this be related ?

Thank you for your help ! Arnaud


Solution

  • The code you provided is fine except one small mistake. You should not add the controller on ons-toolbar-button because it will break the button.

    Just add the controller on the parent element (in initial.html), from this:

    <div class="right">
       <ons-toolbar-button ng-click="menu.toggleMenu()">
          <ons-icon icon="ion-android-share" fixed-width="false"></ons-icon>
       </ons-toolbar-button>
       <ons-toolbar-button ng-controller="AppController" id="android-more" ng-click="popover.show($event);">
          <ons-icon icon="ion-android-more" fixed-width="false"></ons-icon>
       </ons-toolbar-button>
    </div>

    to this:

    <div class="right" ng-controller="AppController" >
       <ons-toolbar-button ng-click="menu.toggleMenu()">
          <ons-icon icon="ion-android-share" fixed-width="false"></ons-icon>
       </ons-toolbar-button>
       <ons-toolbar-button id="android-more" ng-click="popover.show($event);">
          <ons-icon icon="ion-android-more" fixed-width="false"></ons-icon>
       </ons-toolbar-button>
    </div>
    

    You can find a working CodePen example HERE.

    Regarding the sliding-menu, it seems working fine.