javascript - Filter in Angular.js by checking against a scope variable - Stack Overflow

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

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);
});

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);
});
Share Improve this question edited Dec 7, 2013 at 0:27 Dominik H asked Dec 6, 2013 at 23:28 Dominik HDominik H 3132 gold badges6 silver badges15 bronze badges 5
  • 1 If you are trying to get a single object, why are you using ng-repeat? – Fresheyeball 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? – Dominik H Commented Dec 6, 2013 at 23:43
  • Angular two-way-bindings work constantly and in real time, do they not? – Lauri Elias 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 – Dominik H Commented Dec 7, 2013 at 0:04
  • please creat a demo in plunker that gives overview of what you are trying to acplish – charlietfl Commented Dec 7, 2013 at 0:32
Add a ment  | 

1 Answer 1

Reset to default 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/vrwQG/

regards

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773058a4592879.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信