我正在尝试过滤从 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 }
}
最佳答案
假设您有这样定义的路线:
$routeProvider.when('/post/:postId', {...})
然后在您的 Controller 中,您可以从 $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;
});
});
}
关于javascript - 在 angular.js 中过滤 $routeParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166909/