最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - filtering $routeParams in angular.js - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

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;
        });
    });
}
发布评论

评论列表(0)

  1. 暂无评论