最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论