4

I'm trying to get a certain object out of an array of objects, that meets a certain criteria.

The id of the object should equal the scope variable $scope.lobbyid. My current approach below does not pass the lobby object into the controller.

the selected lobby represents the data of the active tab on the site. so the container array is fetched only once on site load.

markup

<div ng-repeat="lobby in lobbies | filter:checkLobbyID(lobby)">
    [[lobby.name]]
</div>

controller

$scope.checkLobbyID = function($lobby) {
    return $lobby.lobbyid == $scope.lobbyid;
}

array

"lobbies": [
    {
        "isglobal": true,
        "lobbyid": 1,
        "name": "GLOBAL",
    },
    {
        "isglobal": false,
        "lobbyid": 2,
        "name": "stackoverflow rules",
    },
    {
        "isglobal": false,
        "lobbyid": 3,
        "name": "sdadadad",
    }
]

a temporary solution is to add the following code to the tab-switch event. but this needs a copy and another scope variable. how to achieve this with a filter?

angular.forEach($scope.lobbies, function(lobby) {
    if (lobby.lobbyid == $scope.lobbyid)
    $scope.currLobby = angular.copy(lobby);
});
5
  • 1
    If you are trying to get a single object, why are you using ng-repeat? Commented Dec 6, 2013 at 23:36
  • 1
    the scope value changes quite often and i want the markup to get the object with the right id automatically. i guess you suppose to create another copy of the object into another scope variable, when updating the array? Commented Dec 6, 2013 at 23:43
  • Angular two-way-bindings work constantly and in real time, do they not? Commented Dec 7, 2013 at 0:02
  • i edited my post. i need to tab through the array of lobbies depending on the current selected id, which is stored at $scope.lobbyid Commented Dec 7, 2013 at 0:04
  • please creat a demo in plunker that gives overview of what you are trying to accomplish Commented Dec 7, 2013 at 0:32

1 Answer 1

6

You dont need the lobby param in the markup. the filter will implicitly pass it to the scope-filter function

<div ng-repeat="lobby in lobbies | filter:checkLobbyID">

http://jsfiddle.net/vrwQG/

regards

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

2 Comments

This is perfect. Exactly what I was looking for
any idea how to pass a param for the checkLobbyID call?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.