0

I am new to Angular JS and have a limited background in JavaScript, so sorry if I am not explaining this right.

I am looking to show an image based upon a value in an array that contains objects. Here is what I have, which works:

<img ng-show="user.Groups[0].Name=='Consumers'" src="images/home.jpg" />

The problem that I am running into is that [0] could also be [1], [2], [3], etc. What can I use to ensure it checks all indexes?

1
  • 2
    Create a function that iterates and checks. Commented Dec 24, 2014 at 1:51

1 Answer 1

1

Here's one way.

Put this in your controller:

$scope.objArrayContains = function(arr, keyName, val) {
    return arr.map(function(a) { return a[keyName]; }).indexOf(val) !== -1;
};

Then your HTML can be like this:

<img ng-show="objArrayContains(user.Groups, 'Name', 'Consumers')" src="images/home.jpg" />
Sign up to request clarification or add additional context in comments.

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.