0
我正在使用angularJS構建訪問http資源的工廠。我可以在請求附近看到本地返回的數據,但數據不會返回到控制器中。這裏是我的工廠:AngularJS:在控制器中返回未定義的值
myNameSpace.factory('simpleFactory', function ($http) {
var factory = {};
var customers = [];
factory.getCustomers = function() {
$http.jsonp('http://URL&callback=JSON_CALLBACK').success(function (data) {
customers = data;
return customers;
})
}
return factory;
});
我的控制器:
myNameSpace.controller('DetailsController', function ($scope, $http, simpleFactory) {
var cust = simpleFactory.getCustomers();
$scope.CustomerData = simpleFactory.getCustomers();
console.log(cust); //The value that is display here is undefined
});
這是一個異步調用! – tymeJV
請您詳細說明一下嗎? – user3486478
該通話需要時間回來 - 因此,當該通話仍在進行中時,您的控制檯語句會被觸發 - 搜索從Angular工廠的異步通話中返回 – tymeJV