tsp*_*ore 114 javascript angularjs
在Angular中,我在范围内有一个返回大量对象的对象.每个都有一个ID(这是存储在一个平面文件中所以没有DB,我似乎无法用户ng-resource)
在我的控制器中:
$scope.fish = [
{category:'freshwater', id:'1', name: 'trout', more:'false'},
{category:'freshwater', id:'2', name:'bass', more:'false'}
];
Run Code Online (Sandbox Code Playgroud)
在我看来,我有更多关于鱼隐藏的信息,默认情况下ng-show更多,但是当我点击简单显示更多标签时,我想调用该功能showdetails(fish.fish_id).我的功能看起来像:
$scope.showdetails = function(fish_id) {
var fish = $scope.fish.get({id: fish_id});
fish.more = true;
}
Run Code Online (Sandbox Code Playgroud)
现在在视图中显示更多详细信息.但是在搜索完文档后,我无法弄清楚如何搜索该fish数组.
那我该怎么查询数组呢?在控制台中如何调用调试器以便我可以使用该$scope对象?
Adr*_*wan 211
您可以使用现有的$ filter服务.我更新了上面的小提琴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';
}
}
Run Code Online (Sandbox Code Playgroud)
Angular文档在这里http://docs.angularjs.org/api/ng.filter:filter
mig*_*ech 95
我知道这是否可以帮到你一点.
这是我试图为你模拟的东西.
结帐jsFiddle;)
http://jsfiddle.net/migontech/gbW8Z/5/
创建了一个过滤器,您也可以在'ng-repeat'中使用它
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;
}
});
Run Code Online (Sandbox Code Playgroud)
控制器中的用法:
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);
}
}]);
Run Code Online (Sandbox Code Playgroud)
如果有任何问题,请告诉我.
小智 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;
}
});
Run Code Online (Sandbox Code Playgroud)
对过滤器的调用将变为:
var found = $filter('getByProperty')('id', fish_id, $scope.fish);
Run Code Online (Sandbox Code Playgroud)
注意,我删除了一元(+)运算符以允许基于字符串的匹配...
Aru*_*hny 13
一个肮脏而简单的解决方案看起来像
$scope.showdetails = function(fish_id) {
angular.forEach($scope.fish, function(fish, key) {
fish.more = fish.id == fish_id;
});
};
Run Code Online (Sandbox Code Playgroud)
你的解决方案是正确的,但不必要复 您可以使用纯javascript过滤功能.这是你的模特:
$scope.fishes = [{category:'freshwater', id:'1', name: 'trout', more:'false'}, {category:'freshwater', id:'2', name:'bass', more:'false'}];
Run Code Online (Sandbox Code Playgroud)
这是你的功能:
$scope.showdetails = function(fish_id){
var found = $scope.fishes.filter({id : fish_id});
return found;
};
Run Code Online (Sandbox Code Playgroud)
你也可以使用表达式:
$scope.showdetails = function(fish_id){
var found = $scope.fishes.filter(function(fish){ return fish.id === fish_id });
return found;
};
Run Code Online (Sandbox Code Playgroud)
有关此功能的更多信息:LINK
| 归档时间: |
|
| 查看次数: |
252686 次 |
| 最近记录: |