Sven B Sven B - 5 months ago 21
AngularJS Question

angular hide button if objects object array contains key

Lets say there is an object(game) that has an array with objects(players) and if the player array has a certain value(_id) that equals the current value I want to hide a button(join).

game object:

{
"_id":"123459595",
"players":[
{
"_id":"some_id",
"name":"some_username"
}
]
}


I know I can hide the button like:

<button ng-hide="playerJoined">Join game</button>


Where playerJoined is a statement that checks if the players is in the array. I have acces to the current player value from the variable currentPlayer that is available from the $rootScope.

How should I check if the currentPlayer exists as a value in the array with players?

Fiddle of example (without the right check).

Answer

You can ng-hide on a $scope.inGame() function that checks $scope.game.players for the given ID. Like so: http://jsfiddle.net/mxhy0sdd/

P.S. Don't use $rootScope to share data between controllers :P I fixed this as well