简体   繁体   中英

dynamic menu items using AngularJS

Let's say I have the following menu structure:

    <li class="dropdown"><img role="button" class="dropdown-toggle" data-toggle="dropdown" ng-src="{{avatarUrl}}" />
      <ul class="dropdown-menu pull-right" role="menu">
        <li ng-hide="user"><a ng-click="openLoginDialog()">Login</a></li>
        <li ng-show="user"><a ng-click="logout()">Logout</a></li>
      </ul>
    </li>

I get the correct menu, but because I'm using ng-show/ng-hide, when I change user = false; programmatically in the controller, the login menu appears. I get why this is happening, but I'm not sure what approach to take when using Angular to prevent it. I tried an ng-repeat:

    <li class="dropdown"><img role="button" class="dropdown-toggle" data-toggle="dropdown" ng-src="{{avatarUrl}}" />
      <ul class="dropdown-menu pull-right" role="menu">
        <li ng-repeat="action in actions"><a ng-click="{{action.command}}">{{action.name}}</li>
      </ul>
    </li>

with:

$scope.actions = [ {
  name : "Login",
  command : "openLoginDialog()"
}, {
  name : "Logout",
  command : "logout()"
} ];

But with that strategy, nothing happens with I click the menu item. I'm not sure what the appropriate Angular approach is to what I'm sure is a pedestrian use case.

Use ng-switch instead of ng-show/hide. ng-switch adds/removes elements from the DOM.

<ul class="dropdown-menu pull-right" role="menu">
    <li ng-switch="user">
       <a ng-switch-when="true" ng-click="logout()">Logout</a>
       <a ng-switch-default     ng-click="openLoginDialog()">Login</a>
    </li>
</ul>

Update to handle @andyczerwonka's comment. Move the ng-switch up one or two levels. If span s inside ul don't bother you:

<ul class="dropdown-menu pull-right" role="menu" ng-switch="user">
  <span ng-switch-when="true">
    <li><a  ng-click="logout()">Logout</a></li>
    <li><a  ng-click="preferences()">Preferences</a></li>
  </span>
  <span ng-switch-default>
    <li><a ng-click="openLoginDialog()">Login</a></li>
  </span>
</ul>

Otherwise, how about one span outside the ul :

<span ng-switch="user">
  <ul class="dropdown-menu pull-right" role="menu" ng-switch-when="true">
      <li><a  ng-click="logout()">Logout</a></li>
      <li><a  ng-click="preferences()">Preferences</a></li>
    </ul>
  <ul class="dropdown-menu pull-right" role="menu" ng-switch-default>
      <li><a ng-click="openLoginDialog()">Login</a></li>
  </ul>
</span>

Fiddle showing both methods.

In my project I use the following setup:

angular.module('mymodule')
.controller('MenuCtrl', function ($scope, $translate, UserService) {

Define actions with roles that are allowed to use these actions:

  $scope.actions = [ {
    name : "menu.login",
    href : "#/login",
    roles : []
  }, {
    name : "menu.logout",
    href : "#/logout",
    roles : [ 'READ_ONLY', 'ADMIN' ]
  }, {
    name : "menu.foo",
    href : "#/foo",
    roles : [ 'READ_ONLY', 'ADMIN' ]
  }, {
    name : "menu.adminArea",
    href : "#/adminArea",
    roles : [ 'ADMIN' ]
  } ];

Define an isAllowed function that determines if an action can be used by the current user:

  $scope.isAllowed = function(action) {
    if (action.roles && action.roles.length) {
      for ( var i = 0; i < action.roles.length; ++i) {
        if (jQuery.inArray(action.roles[i], UserService.roles) >= 0) {
          return true;
        }
      }
      return false;
    } else {
      return !(UserService.roles && UserService.roles.length);
    }
  };

Then use $roueChangeSuccess event to determine which actions should be displayed for the current user:

  $scope.initView = function() {
    for(var i=0; i<$scope.actions.length; ++i) {
      var action = $scope.actions[i];
      if($scope.isAllowed(action)) {
        action.isRendered = true;
      } else {
        action.isRendered = false;
      }
    }
  };

  $scope.$on('$routeChangeSuccess', function() {
    $scope.initView();
  });

});

Finally, this is my menu template:

<nav ng-controller="MenuCtrl">
  <ul>
    <span ng-repeat="action in actions" ng-switch on="action.isRendered">
      <li ng-switch-when="true"><a href="{{action.href}}">{{action.name | translate}
      </a></li>
    </span>
  </ul>
</nav>

Why don't you assign functions to your commands:

var openLoginDialog = function() { ... };
var logout = function() { ... };

$scope.actions = [ {
  name : "Login",
  command : openLoginDialog
}, {
  name : "Logout",
  command : logout
} ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM