我是 AngularJS 的新手。我试图从工厂函数调用 json 文件,但它显示我的 varcustomers = [];
数组未更新。请帮忙。
下面是我的代码。
var demoApp = angular.module("myApp", []);
demoApp.factory('simpleFactory', ['$http', function($http){
return {
getCustomer: function(){
var customers = [];
return $http.get("http://www.w3schools.com/website/Customers_JSON.php")
.success(
function(data){
customers = data;
},
function(error){
console.log(error);
});
console.log(customers)
}
}
}]);
demoApp.controller("emplyCtrl", function($scope, simpleFactory){
$scope.customers = simpleFactory.getCustomer();
});
最佳答案
尝试这样的事情:
demoApp.factory('simpleFactory', ['$http', function($http){
return {
getCustomer: function(){
return $http.get("json/customers.json").then(
function(response){
var customers = [];
customers = response.data;
},
function(error){
console.log(error);
});
}
}
}]);
关于javascript - 从 Angular Factory 调用 JSON 文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216769/