2013-03-25 103 views
113

在Angular中,我在范围内有一个返回大量对象的对象。每个都有一个ID(这被存储在平面文件,所以没有DB和我似乎无法用户ng-resource在Angular中,我需要搜索一个数组中的对象

在我的控制器:

$scope.fish = [ 
    {category:'freshwater', id:'1', name: 'trout', more:'false'}, 
    {category:'freshwater', id:'2', name:'bass', more:'false'} 
]; 

我认为我对附加信息默认情况下鱼与ng-show更隐藏,但是当我点击简单显示更多选项卡时,我想调用功能showdetails(fish.fish_id)。 我的功能看起来是这样的:

$scope.showdetails = function(fish_id) { 
    var fish = $scope.fish.get({id: fish_id}); 
    fish.more = true; 
} 

现在在视图中的详细信息显示出来。然而,通过搜索文件后,我无法弄清楚如何搜索fish阵列。

那么如何查询数组?并且在控制台中,我如何调用调试程序,以便我可以使用$scope对象?

回答

95

我知道这可以帮助你一点。

这是我试图为你模拟的东西。

结帐了的jsfiddle;)

http://jsfiddle.net/migontech/gbW8Z/5/

创建一个过滤器,你也可以在 'NG重复' 在控制器

app.filter('getById', function() { 
    return function(input, id) { 
    var i=0, len=input.length; 
    for (; i<len; i++) { 
     if (+input[i].id == +id) { 
     return input[i]; 
     } 
    } 
    return null; 
    } 
}); 

用途使用:

app.controller('SomeController', ['$scope', '$filter', function($scope, $filter) { 
    $scope.fish = [{category:'freshwater', id:'1', name: 'trout', more:'false'}, {category:'freshwater', id:'2', name:'bass', more:'false'}] 

    $scope.showdetails = function(fish_id){ 
     var found = $filter('getById')($scope.fish, fish_id); 
     console.log(found); 
     $scope.selected = JSON.stringify(found); 
    } 
}]); 

如果有任何问题,请让我知道。

+0

作为进出口新的角度和JavaScript,我没有得到的 '+' 的含义 “如果(+输入[I] .ID == + ID){” 语句,请你分享你的想法。 – Harshavardhan 2016-08-10 15:01:45

+0

完美解决方案! – 2016-10-05 10:31:07

+1

我认为“+ input [i] .id == + id”确保您比较数字。因此,您可以将1或'1'传递给$ filter,它的行为方式完全相同。 我使用的是字母数字Ids,所以我将其更改为“input [i] .id === id” – 2016-11-10 13:00:37

13

一个肮脏的和简单的解决方案可能看起来像

$scope.showdetails = function(fish_id) { 
    angular.forEach($scope.fish, function(fish, key) { 
     fish.more = fish.id == fish_id; 
    }); 
}; 
+5

为什么这是_dirty_解决方案? – Trialcoder 2014-09-01 07:06:07

+5

我的猜测是因为可能会有10亿条鱼的记录,我们只是逐一循环浏览它们 – RedactedProfile 2014-12-22 21:08:35

+6

其他语言中会有更多的记录。我的意思是,C++中有很多鱼。 (哦,闭嘴..我会去投票自己.. !!) – 2016-04-06 11:50:48

210

您可以使用现有的$过滤服务。我更新了上面http://jsfiddle.net/gbW8Z/12/

$scope.showdetails = function(fish_id) { 
    var found = $filter('filter')($scope.fish, {id: fish_id}, true); 
    if (found.length) { 
     $scope.selected = JSON.stringify(found[0]); 
    } else { 
     $scope.selected = 'Not found'; 
    } 
} 

角文档的小提琴是在这里http://docs.angularjs.org/api/ng.filter:filter

+4

Ofcourse - 比重写它再容易得多。 – 2014-04-03 08:58:24

+2

非常有帮助 - 当我学习角度时,我意识到我需要停止思考jQuery函数(试图让$ .grep来做到这一点) - 而不是使用这个$ filter服务正是我所需要的! – Bobby 2015-01-24 05:00:23

+2

更好的答案,因为它不涉及编写自己的过滤器。 – stevenw00 2015-03-05 04:11:34

22

要添加到@ migontech的回答,也是他的地址他的评论,你可以“可能使它更通用的”,这里有一种方法可以做到它。以下将允许您通过任何财产搜索:然后

.filter('getByProperty', function() { 
    return function(propertyName, propertyValue, collection) { 
     var i=0, len=collection.length; 
     for (; i<len; i++) { 
      if (collection[i][propertyName] == +propertyValue) { 
       return collection[i]; 
      } 
     } 
     return null; 
    } 
}); 

过滤的调用将变为:

var found = $filter('getByProperty')('id', fish_id, $scope.fish); 

注意,我删除了一元(+)运算符来实现基于字符串匹配...

+0

考虑到文档状态,这是ng-init的唯一用例,我会说这绝对是Angular的方式。 – bluehallu 2014-02-20 09:58:50

+0

很容易理解的例子和非常有帮助 – rainabba 2014-09-16 07:29:45

7

你的解决方案是正确的,但不必要的复杂。您可以使用纯粹的javascript过滤功能。这是你的模型:

 $scope.fishes = [{category:'freshwater', id:'1', name: 'trout', more:'false'}, {category:'freshwater', id:'2', name:'bass', more:'false'}]; 

这是你的函数:

 $scope.showdetails = function(fish_id){ 
     var found = $scope.fishes.filter({id : fish_id}); 
     return found; 
    }; 

您还可以使用表达式:

 $scope.showdetails = function(fish_id){ 
     var found = $scope.fishes.filter(function(fish){ return fish.id === fish_id }); 
     return found; 
    }; 

更多关于此功能:LINK

4

看到这个线程,但我想搜索与我的搜索不匹配的ID。代码做到这一点:

found = $filter('filter')($scope.fish, {id: '!fish_id'}, false); 
+0

这对我有效。简单,光滑,易于测试。谢谢! – 2017-05-05 15:09:25