Search code examples
angularjsangular-materialangular-ng-if

Flickering with dynamic <md-menu-content> list


I want to populate md-menu-content with a dynamic list based on whether the user is logged or not. The code:

     <md-menu>
        <md-button class="md-icon-button" aria-label="Account" ng-click="$mdOpenMenu($event)">
            <md-icon md-svg-icon="~/../Content/img/icons/ic_account_box_white_48px.svg" class="s48" aria-label="Account"></md-icon>
        </md-button>
        <md-menu-content width="4">
            <md-menu-item ng-if-start="vm.user.isLogged">
                <p>Logged as: {{ vm.user.username }}</p>
            </md-menu-item>
            <md-menu-divider></md-menu-divider>
            <md-menu-item>
                <md-button ng-click="vm.myProfile($event)">
                    My profile
                </md-button>
            </md-menu-item>
            <md-menu-item ng-if-end>
                <md-button ng-click="vm.logout($event)">
                    Logout
                </md-button>
            </md-menu-item>

            <md-menu-item ng-if-start="!vm.user.isLogged">
                <md-button ng-click="vm.openRightSideNav('login')">
                    Login
                </md-button>
            </md-menu-item>
            <md-menu-item>
                <md-button ng-click="vm.openRightSideNav('register')">
                    Register
                </md-button>
            </md-menu-item>
            <md-menu-item class="menu-item-facebook" ng-if-end>
                <md-button ng-click="vm.openRightSideNav('register')">
                    <span class="icon-button-facebook"></span>
                    Log in with Facebook
                </md-button>
            </md-menu-item>
        </md-menu-content>
    </md-menu>

This works, but there is a flicker once the user logs out. I mean, for a split second all menu items are being shown. Using ng-switch removes the flickering, but extra dom element breaks the menu:

         <md-menu-content width="4">
            <ng-switch on="vm.user.isLogged">
                <span ng-switch-when="true">
                ...
                </span>
                <span ng-switch-default>
                    ...
                </span>
            </ng-switch>
        </md-menu-content>

Any suggestions how to deal with the situation?


Solution

  • You are right. There is a flickering in your case. I tried to implement in many different ways and got one solution. It is not the best solution I must say but there will not be any flickering. You just try to use two seperate menu for logIn and logOut.

    Here is the example code.

    <md-menu ng-if="vm.user.isLogged">
     //md-menu-item
    </md-menu>
    <md-menu ng-if="!vm.user.isLogged">
     //md-menu-item
    </md-menu>
    

    Here is a working example. http://codepen.io/next1/pen/wWzebw