Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

javascript - 在Angular中,我需要搜索数组中的对象(In Angular, I need to search objects in an array)

In Angular, I have in scope a object which returns lots of objects.(在Angular中,我在范围内有一个返回大量对象的对象。)

Each has an ID (this is stored in a flat file so no DB, and I seem to not be able to user ng-resource )(每个都有一个ID(这是存储在一个平面文件中,所以没有DB,我似乎无法使用ng-resource )) In my controller:(在我的控制器中:) $scope.fish = [ {category:'freshwater', id:'1', name: 'trout', more:'false'}, {category:'freshwater', id:'2', name:'bass', more:'false'} ]; In my view I have additional information about the fish hidden by default with the ng-show more, but when I click the simple show more tab, I would like to call the function showdetails(fish.fish_id) .(在我看来,我有关于默认隐藏的鱼的更多信息,更多的ng-show ,但是当我点击简单显示更多选项卡时,我想调用showdetails(fish.fish_id)函数。) My function would look something like:(我的功能看起来像:) $scope.showdetails = function(fish_id) { var fish = $scope.fish.get({id: fish_id}); fish.more = true; } Now in the the view the more details shows up.(现在在视图中显示更多详细信息。) However after searching through the documentation I can't figure out how to search that fish array.(然而,在搜索完文档之后,我无法弄清楚如何搜索该fish 。) So how do I query the array?(那我该怎么查询数组呢?) And in console how do I call debugger so that I have the $scope object to play with?(在控制台中如何调用调试器以便我可以使用$scope对象?)   ask by tspore translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the existing $filter service.(您可以使用现有的$ filter服务。)

I updated the fiddle above http://jsfiddle.net/gbW8Z/12/(我更新了上面的小提琴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'; } } Angular documentation is here http://docs.angularjs.org/api/ng.filter:filter(Angular文档在这里http://docs.angularjs.org/api/ng.filter:filter)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...