在 Angular 中,我在范围内有一个返回大量对象的对象。每个都有一个 ID(这是存储在一个平面文件中,所以没有数据库,而且我似乎无法使用 ng-resource
)
在我的 Controller 中:
$scope.fish = [
{category:'freshwater', id:'1', name: 'trout', more:'false'},
{category:'freshwater', id:'2', name:'bass', more:'false'}
];
在我看来,我有更多关于鱼的额外信息,默认情况下使用 ng-show
more,但是当我单击简单的显示更多选项卡时,我想调用函数 显示详细信息(fish.fish_id)
。
我的函数看起来像:
$scope.showdetails = function(fish_id) {
var fish = $scope.fish.get({id: fish_id});
fish.more = true;
}
现在在 View 中显示了更多详细信息。但是,在搜索完文档后,我不知道如何搜索那个 fish
数组。
那么如何查询数组呢?在控制台中,我如何调用调试器以便我可以使用 $scope
对象?
最佳答案
您可以使用现有的 $filter 服务。我更新了上面的 fiddle 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 文档在这里 http://docs.angularjs.org/api/ng.filter:filter
关于javascript - 在 Angular 中,我需要在数组中搜索对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610501/