我有以下格式的Json文件: json文件的名称是 discover.json ,路径是 json-files / discover.json
{"data": [
{"username": "aky123",
"name": "ajay"},
{"username": "sky123",
"name": "sanjay"}
]}
我的工厂是:
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'
})
并在控制器中:
myAppControllers.controller('ProfileListCtrl',['$scope', 'ProfileData', '$timeout', function($scope, ProfileData, $timeout) {
$scope.profile=ProfileData.query();
console.log($scope.profile);
}]);
$ scope.profile无法从JSON文件中提取数据,我无法理解我的错误,所以请帮助我..
答案 0 :(得分:1)
你没有有效的json
有效的json是:
{
"data": [{
"username": "aky123",
"name": "ajay"
}, {
"username": "sky123",
"name": "sanjay"
}]
}
答案 1 :(得分:0)
此代码适用于我的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>