有人可以幫我使用這段代碼嗎?我在控制器返回與返回值的問題,函數只AngularJS服務不會返回來自http.get的值
var products = {"id": 3};
我想收集http.get值,可有人告訴我該怎麼做? 控制器:
$scope.product = {};
$scope.init = function() {
$scope.loadProducts()
}
$scope.loadProducts = function() {
// $http.get("/products/list").then(function (resp) {
// $scope.products = resp.data;
// })
$scope.products = getListProducts.loadProducts();
}
服務
var myServices = angular.module('myServices', []);
myServices.service('getListProducts', ['$http', function ($http) {
var products = {"id": 3};
this.loadProducts = function() {
$http.get("/products/list").then(function (resp) {
products = resp.data;
})
return products;
}
}]);
可能重複[如何從異步調用返回響應?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-打電話) –
*沒有有效的網址*? –