我有以下格式的Json文件:
json文件的名称是discover.json,path是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文件中提取数据,我无法理解我的错误,所以请帮助我..
最佳答案 此代码适用于我的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>