Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally hide menu button in ionic framework outside <ion-view>

In the ionic framework I am trying to hide the menu button conditionally. For other reasons I had to split the menu in its own controller (I don't want to completly re-render the menu and header bar on a refresh), so the header is not in ion-view. I created a watcher on the conditional variable (a stateparam) in the controller of the header. The console log outputs the conditional variable correctly, but the view is not updated (the menu button is always showing).

This is the template:

<ion-side-menus>
    <ion-side-menu-content>
        <ion-nav-bar class="bar-stable nav-title-slide-ios7">
            <ion-nav-back-button ng-if="!isHome" class="button-clear"><i class="icon ion-ios7-arrow-back"></i>Back</ion-nav-back-button>
            <button ng-if="isHome" menu-toggle="left" class="button button-icon icon ion-navicon"></button>
            <h1 class="title">Title</h1>

        </ion-nav-bar>
    <ion-nav-view name="menuContent" animation="slide-left-right"></ion-nav-view>
</ion-side-menu-content>

...
</ion-side-menus>

And in the controller:

$scope.$watch(function(){
    return $stateParams.contentUrl;
},
    function(newVal){
        console.log(newVal);
        if(!newVal || newVal === 'someParam'){
            $timeout(function(){
                $scope.$apply(function(){
                    $scope.isHome = true;
                });
                console.log("home");
            });
        } else {
            $timeout(function(){
                $scope.$apply(function(){
                    $scope.isHome = false;
                });
                console.log("not home");
            });
        }
    });

Is there an easier way? Or am I missing something here?

like image 438
mwiegboldt Avatar asked Nov 11 '14 15:11

mwiegboldt


People also ask

How do I hide my ion menu?

If the menu is disabled or it's being presented as a split-pane, the menu is marked as non-active and ion-menu-toggle hides itself. In case it's desired to keep ion-menu-toggle always visible, the autoHide property can be set to false . See the Menu documentation for an example.

How do I change the menu icon in ionic?

To define a custom asset for our ion-icon is as easy as just passing the path of our file in the src property. Now copy and paste the XML below into the custom-menu. svg . The cool part is that using this technique it's possible to customize the theme by just adding the color attribute in the ion-menu-button .

What is the purpose of using ion menu toggle </ ion menu toggle tag on the side menu?

The MenuToggle component can be used to toggle a menu open or closed. By default, it's only visible when the selected menu is active. A menu is active when it can be opened/closed.

How do you close the side menu in ionic 4?

The <ion-menu-toggle> component is used to open and close the side menu. So when you click on the menu, it will close the side menu automatically.


2 Answers

You can use the hide-back-button attribute on the <ion-view> element to declare if that view should hide the back button by default.

http://ionicframework.com/docs/nightly/api/directive/ionView/

<ion-view hide-back-button="true">
  <!-- view contents -->
</ion-view>
like image 177
Jeremy Wilken Avatar answered Oct 30 '22 13:10

Jeremy Wilken


A kinda dirty workaround would be to give those buttons an id and then use jqlite to hide them like this in the controller:

angular.element(document.querySelector('#back-button')).addClass('hidden');
like image 35
mwiegboldt Avatar answered Oct 30 '22 13:10

mwiegboldt