0

I'm using angular to build a small web application. I'm trying to set up role based navigation. It seems as if the isAdmin function is not getting called on page load, as I can just see the foo anchor tag.

HTML

<body ng-controller='AccountController'>
    <nav class="navbar navbar-default">
      <ul>
          <li><a href='google.com'>foo</a></li>
          <li ng-if="isAdmin()"><a href='google.com'>bar</a></li>
      </ul>
    </nav>
</body>

AccountController

var app = angular.module('appControllers', []);

app.controller = angular.controller('AccountController', ['$scope', 
function($scope, $http) {
    $scope.isAdmin = function() {
        return true; //Just as a test to make sure it works
    }
}]);

Ideally this will hit a web service that will return the administrator status, but for now I'd like to get this to work.

Thanks for all the help in advance,

Andres

7
  • Refer to - arthur.gonigberg.com/2013/06/29/angularjs-role-based-auth Commented Oct 6, 2014 at 20:15
  • 1
    You sure that there can't be an error in the JS you didn't show us? :) Are there any JS Errors? Commented Oct 6, 2014 at 20:26
  • Sadly, no JS errors, nothing popped in the console. Commented Oct 6, 2014 at 20:52
  • @AndresL Are you positive there are no console errors? I would expect at least a type error along the lines of undefined is not a function (Chrome). Is your isAdmin function declared anywhere else on your scope? Commented Oct 6, 2014 at 21:37
  • 1
    @AndresL Your code appears to work for the most part for me... see: this plunker . All that I did was change the controller declaration and inject the missing $http. It appears that you only want this item to generate based on your ng-if condition. Am I mis-understanding your question? Commented Oct 8, 2014 at 20:52

2 Answers 2

1

In your example you need to remove braces to make it work. But I usually use custom directive to make it work. Example:

<div class="somediv" show-for-role="ROLE_A,ROLE_B"></div> 

And if your user has some of these roles your can manage it.

appModule.directive('manageAccess', ['someState', function (appState) {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      var user = someState.currentUser;
      scope.$watch(someState.watchCurrentUser, function(n,o){
         if (n === o){
           return;
         }
        hideShow(element,attrs,n);
      });
      hideShow(element,attrs,user);
    }
  }

var hideShow = function(element,attrs,user){
element.hide();
if (user) {
  var role = attrs.showForRole;
  if (role){
    role.split(',').forEach(function(it){
      if (it == user.role){
        element.show();
      }
    })
  }
}};
Sign up to request clarification or add additional context in comments.

1 Comment

I have yet to use a custom directive, so this should be an adventure. I'll give it a try tomorrow when I get back into work. I'll post an update then, thanks Artemis
0

Try to remove extra '()' after 'isAdmin' like that :

function($scope, $http) {
    $scope.isAdmin = function() {
        return true; //Just as a test to make sure it works
    }
}

Hope it helps...

2 Comments

Your welcome. Don't forget to mark the response in the case of someone else get the same issue as you ! Let me know if it works
I mistyped it, I had already removed the parentheses.. I'll edit the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.