我正在尝试过滤从json文件返回的数据的$ routeParams。下面是我的代码:
function PostDetailController($scope, $routeParams, $http) {
$http.get('json/posts.json').success(function(data){
$scope.photo = data;
});
}
如何过滤给定网址的ID?下面是我的json看起来像
{
"id": 1,
"name": "details1",
"title": "Test Title",
"description": "Sample Description",
"image": "img/1.jpg",
"tags": ["photo", "internet"],
"user": "hilarl",
"comments": ["sample comment"],
"likes": 23,
"dislikes": 100,
"resolution": { "x": 150, "y": 58 }
},
{
"id": 2,
"name": "details2",
"title": "Test Title",
"description": "Sample Description",
"image": "img/2.jpg",
"tags": ["photo", "internet"],
"user": "hilarl",
"comments": ["sample comment"],
"likes": 23,
"dislikes": 100,
"resolution": { "x": 150, "y": 58 }
}
答案 0 :(得分:9)
假设你有一条这样定义的路线:
$routeProvider.when('/post/:postId', {...})
然后在您的控制器中,您可以从$ routeParams中检索id:
function PostDetailController($scope, $routeParams, $http) {
$http.get('json/posts.json').success(function(data){
angular.forEach(data, function(item) {
if (item.id == $routeParams.postId)
$scope.photo = item;
});
});
}