4

I have a Json file in following format: name of json file is discover.json and path is json-files/discover.json

    {"data": [
{"username": "aky123", 
    "name": "ajay"}, 
    {"username": "sky123",
     "name": "sanjay"}
    ]}

my factory is:

var myAppServices=angular.module('myAppServices',['ngResource']);

myAppServices.factory('ProfileData',['$resource', function($resource){
    return $resource('/discover/:username.json', {}, {
        query: {method: 'GET' , params: {username: 'json-files/discover' }, isArray: true }
    });
}]);

App.js :

myApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/discover', {
        templateUrl: 'partials/home-page.html',
        controller: 'ProfileListCtrl'
      }).
      when('/discover/:username', {
        templateUrl: 'partials/profile-detail.html',
        controller: 'ProfileDetailCtrl'
      })

and in controller:

myAppControllers.controller('ProfileListCtrl',['$scope', 'ProfileData', '$timeout', function($scope, ProfileData, $timeout) {
        $scope.profile=ProfileData.query();
    console.log($scope.profile);
}]);

$scope.profile is not able to pull data from JSON file, I am not able to understand my mistake here so please help me..

1
  • 1
    "name of json file is discover.json" if the filename is discover.json then why are you using '/discover/:username.json'? Commented Feb 1, 2016 at 14:34

2 Answers 2

1

you have not valid json

valid json is:

 {
    "data": [{
        "username": "aky123",
        "name": "ajay"
    }, {
        "username": "sky123",
        "name": "sanjay"
    }]
}
Sign up to request clarification or add additional context in comments.

2 Comments

To add to this, when debugging anything with a JSON return, put your JSON into jsonlint for a quick smoke-test.
@Mike88 I corrected my Json format but still not able to pull data. I think there are some error in service.js , if u will help me in that section then its great. :)
0

This code works in my localhost

//Service.js
myAppServices.factory('ProfileData',['$resource', function($resource){
return $resource('/discover/:username.json', {},{
'query': {method: 'GET',{}, isArray: false }
});
}]);
//Controller.js
var promise = ProfileData.query();
promise.$promise.then(function(response) {
$scope.data = response.data;
});
//view.html
<li ng-repeat="name in data">
    {{name.username}}
</li>      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.