简体   繁体   中英

AngularJS passing a parameter in an $http.get() to get a subset of a json file

I am currently stubbing my $http.get() calls with json files. I am wanting to return a subset of the json file to angular controller. I have looked at other post and they suggested setting a params property in the Get call but it is not working for me.

Here is my controller:

 angular.module('agileApp')
.controller('projectController', ['$scope', '$filter', '$http','$routeParams', function($scope, $filter, $http, $routeParams){

$http.get('json/testData.json').success(function(data){

    //console.log(data);
    $scope.projectData = $filter('filter')(data.projectDetail, {Id:1})[0];
    console.log($scope.projectData);
});

}]);

Here is my json file:

{ 'projectDetail': [{
    'Id': 1,
    'projectName': 'Project1',
    'projectStatus': 'In Progress',
    'teamMembers': ['User1,'User2', 'User3'],
    'userStories': [
        {'userStoryId': 0,
         'Description': 'As a user blah blah blah 1',
         'storyPoints': 10
        },
        {'userStoryId': 1,
         'Description': 'As a user blah blah blah 2',
         'storyPoints': 10
        },
        {'userStoryId': 2,
         'Description': 'As a user blah blah blah 3',
         'storyPoints': 10
        },
        {'userStoryId': 3,
         'Description': 'As a user blah blah blah 4',
         'storyPoints': 10
        },
        {'userStoryId': 4,
         'Description': 'As a user blah blah blah 5',
         'storyPoints': 10
        },
        {'userStoryId': 5,
         'Description': 'As a user blah blah blah 6',
         'storyPoints': 10
        },
    ]
},

There are other objects in the project detail array, but I just want to pull back the one where Id = routeID which is passed into the controller as a parameter.

Any help would be greatly appreciated.

Thanks!

Your json is invalid, so $http.get gives you a string instead of an object

There are some syntax errors and overmore angular requires proper quotation (different to js in common).

I testet a simple set with this plunker

Here is my simplified json. I think you can easily figure out how to adopt yours. (It's " intead of ')

{
  "projectDetail": [
    {
      "Id": 1,
      "projectName": "Test1"
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM