1

Does anyoen know how to check (in the View) for a presence of id in a given array?

Lets say I have an array like so:

var arr = [123, 456, 789];

and a data source like this:

var data = [
  {id: 123, name: 'foo'},
  {id: 456, name: 'bar'},
  {id: 789, name: 'baz'}
];

Now in the view while iterating over the data array I would like to show/hide elements based on a presence of id in the arr like so:

<div ng-repeat="item in data">
    <span ng-show="item.id in arr"></span>
</div>

The above code item.i in arr of course does not work for obvious reasons. Anyone know how to achieve that functionality? Not to mention the ng-show block should always kick-in whenever the arr array is altered.

Thanks in advance.

1
  • Any particular reason why this question was downvoted? Commented Jul 1, 2013 at 14:32

1 Answer 1

3

You can just create a method in your scope

function MyCtrl( $scope ) {
  $scope.data = ...

  $scope.enabled = ...


  $scope.visible = function (id ) { 
    return $scope.enabled.indexOf(id) > -1
  } 

}

and

<span ng-show="visible(item.id)" ></span>
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.