我有一个 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 }
});
}]);
应用程序.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'
})
在 Controller 中:
myAppControllers.controller('ProfileListCtrl',['$scope', 'ProfileData', '$timeout', function($scope, ProfileData, $timeout) {
$scope.profile=ProfileData.query();
console.log($scope.profile);
}]);
$scope.profile 无法从 JSON 文件中提取数据,我无法理解我的错误,所以请帮助我..
最佳答案
你没有有效的json
有效的 json 是:
{
"data": [{
"username": "aky123",
"name": "ajay"
}, {
"username": "sky123",
"name": "sanjay"
}]
}
关于javascript - 无法从工厂 AngularJS 中的 JSON 文件中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133061/