Failed to extract data from JSON file in AngularJS factory

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

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

my factory:

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 the controller:

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

$ scope.profile cannot extract data from a JSON file, I cannot understand my error here, so please help me ..

+4
source share
2 answers

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>      
0
source

you have invalid json

valid json:

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

Source: https://habr.com/ru/post/1626867/


All Articles