0

I have a function that checks for userPermissions and it is part of a Service called MyService . I am passing in an array with only one string. The issue I am facing is angular.forEach is looping through character by character instead of the complete string. Please can you advise?

//Service changes

MyService.userHasPermission("['Read']")

//Permission function

 function userHasPermission (permissions){
        var found = false;
        angular.forEach(permissions, function(expectedPermission, index){
            if ($localStorage.userPermissions.indexOf(expectedPermission) >= 0){
                found = true;
                return;
            }
        });

        return found;
    };
4
  • 2
    You are passing a string and not an array Commented Mar 14, 2017 at 10:21
  • You need to pass array. Not a string Commented Mar 14, 2017 at 10:23
  • Exactly. You need to do MyService.userHasPermission(['Read']) Commented Mar 14, 2017 at 10:23
  • oh! I get it now, thanks a lot guys for your help. Commented Mar 14, 2017 at 10:24

2 Answers 2

0

"abcde".forEach() will loop through every letter. That's how forEach works. "['Read']".forEach() will just go through [,',R,e,a,d,',].

MyService.userHasPermission(['Read']) will work.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to pass in array instead of string. Use MyService.userHasPermission(['Read']) instead of MyService.userHasPermission("['Read']"). angular.forEach takes in array as first parameter which in your case is character array so you will either need to pass in array of strings for your code to work as expected.

Comments

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.