Angular js 资源调用以获取数组中的 json 数据
Angular js resource call to get json data in array
创建了将 return json 数据并从控制器调用它的工厂,但它正在变空。
不知道我哪里弄错了。
没有 console 错误并且在 network json 中也正在加载。
'use strict';
var app = angular.module('angularJson', ['ngResource']);
app.factory('loadJsonService', ['$resource', function ($resource) {
return {
getData: function () {
return $resource('json/productDetails.json');
}
};
}])
app.controller('angularJsonCtrl',['$scope', 'loadJsonService',function($scope, loadJsonService){
$scope.loadProducts = function(noOfData){
$scope.productDetails = [];
$scope.productDetails = loadJsonService.getData().query();
}
}]);
您必须像这样在 getData 函数中设置 $http 请求
$http.jsonp("json/productDetails.json")
.success(function(response) {
callback(response);
});
您必须等待请求完成,然后您需要使用 .$promise.then
而不是 $resource.query
函数调用。这样你就可以放置将在 API 调用成功时调用的函数。
loadJsonService.getData().query().$promise.then(function(res){ //success function
$scope.productDetails = res;
}, function(error){ //error function
console.log(error);
})
尝试内置 query
方法。
如果您想编写自己的 get 方法,请执行以下操作,
{
method: 'GET',
params: {
key: value
},
isArray: true,
cache: true // if you want to cache
}
创建了将 return json 数据并从控制器调用它的工厂,但它正在变空。 不知道我哪里弄错了。 没有 console 错误并且在 network json 中也正在加载。
'use strict';
var app = angular.module('angularJson', ['ngResource']);
app.factory('loadJsonService', ['$resource', function ($resource) {
return {
getData: function () {
return $resource('json/productDetails.json');
}
};
}])
app.controller('angularJsonCtrl',['$scope', 'loadJsonService',function($scope, loadJsonService){
$scope.loadProducts = function(noOfData){
$scope.productDetails = [];
$scope.productDetails = loadJsonService.getData().query();
}
}]);
您必须像这样在 getData 函数中设置 $http 请求
$http.jsonp("json/productDetails.json")
.success(function(response) {
callback(response);
});
您必须等待请求完成,然后您需要使用 .$promise.then
而不是 $resource.query
函数调用。这样你就可以放置将在 API 调用成功时调用的函数。
loadJsonService.getData().query().$promise.then(function(res){ //success function
$scope.productDetails = res;
}, function(error){ //error function
console.log(error);
})
尝试内置 query
方法。
如果您想编写自己的 get 方法,请执行以下操作,
{
method: 'GET',
params: {
key: value
},
isArray: true,
cache: true // if you want to cache
}