根据用户权限显示/隐藏菜单

Show / hide menu according to user privilege

我有一个 Ionic 应用程序,它有选项卡 + 菜单,这个问题特定于菜单。

我在 index.html 中这样定义我的侧边菜单

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/routes.js"></script>
    <script src="js/services.js"></script>
    <script src="js/directives.js"></script>

  </head>
  <body ng-app="app" animation="slide-left-right-ios7">
  <div>

<ion-side-menus enable-menu-with-back-views="false" data-componentid="side-menu21">
    <ion-side-menu-content>
        <ion-nav-bar class="bar-assertive">
            <ion-nav-back-button class="button-icon icon ion-ios-arrow-back"> </ion-nav-back-button>

            <ion-nav-buttons side="left">
                <button class="button button-icon button-clear ion-navicon" menu-toggle="left"></button>
            </ion-nav-buttons>

            <ion-nav-buttons side="right">
                <button class="button button-icon button-clear ion-android-notifications" menu-toggle="right" ></button>
            </ion-nav-buttons>

        </ion-nav-bar>
        <ion-nav-view></ion-nav-view>
    </ion-side-menu-content>

    <ion-side-menu side="left" id="side-menu21">

        <ion-header-bar class="bar-assertive">
            <div class="title">Menu</div>
        </ion-header-bar>

        <ion-content padding="false" class="side-menu-left has-header">

            <ion-list id="menu-list2" class=" " data-componentid="list2">

                <ion-item id="menu-list-item1" ui-sref="myProfile"> 
                  <i class="icon ion-person" ></i> My Profile </ion-item>

                <ion-item id="menu-list-item15" ui-sref="broadcast "> 
                  <i class="icon ion-contrast" ></i> Broadcast </ion-item>

            </ion-list>

        </ion-content>

    </ion-side-menu>
</ion-side-menus>

</div>
  </body>
</html>

我有一项服务在 登录 时触发,这给我带来了权限

{ "authorities": 
       [
         "myProfile","broadcast"
       ]
}

我稍后会像这样存储在 localstorage

window.localStorage.setItem("authority", data.authorities);

我想做的是,根据我从登录获得的权限动态显示或隐藏菜单?

例如,在当局中,如果没有 "myProfile","broadcast" 而只有 "myProfile",则广播不应显示在侧边菜单中。

您可以使用 ng-hideng-if 指令。

<ion-item ng-if="hasAuthority('broadcast')" id="menu-list-item15" ui-sref="broadcast "> 
                  <i class="icon ion-contrast" ></i> Broadcast
</ion-item>

然后在controller中有一个函数

$scope.hasAuthority = function(auth){
    //code to check if localstorage has needed permission
    //return true or false
}