i'm trying to filter the $routeParams of the data returned from the json file. below is my code:
function PostDetailController($scope, $routeParams, $http) {
$http.get('json/posts.json').success(function(data){
$scope.photo = data;
});
}
how do i filter the id of the given url? below is what my json look like
{
"id": 1,
"name": "details1",
"title": "Test Title",
"description": "Sample Description",
"image": "img/1.jpg",
"tags": ["photo", "internet"],
"user": "hilarl",
"ments": ["sample ment"],
"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",
"ments": ["sample ment"],
"likes": 23,
"dislikes": 100,
"resolution": { "x": 150, "y": 58 }
}
i'm trying to filter the $routeParams of the data returned from the json file. below is my code:
function PostDetailController($scope, $routeParams, $http) {
$http.get('json/posts.json').success(function(data){
$scope.photo = data;
});
}
how do i filter the id of the given url? below is what my json look like
{
"id": 1,
"name": "details1",
"title": "Test Title",
"description": "Sample Description",
"image": "img/1.jpg",
"tags": ["photo", "internet"],
"user": "hilarl",
"ments": ["sample ment"],
"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",
"ments": ["sample ment"],
"likes": 23,
"dislikes": 100,
"resolution": { "x": 150, "y": 58 }
}
Share
Improve this question
asked Jun 23, 2012 at 4:42
hilarlhilarl
6,88013 gold badges51 silver badges58 bronze badges
1 Answer
Reset to default 9Let's say you have a route defined like this:
$routeProvider.when('/post/:postId', {...})
Then in your controller you can retrieve the id from $routeParams:
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;
});
});
}