filtering $routeParams in angular.js

angularjs, javascript

Solution

Let'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;
        });
    });
}

Problem

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", "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 } } ```

Original source