如何在customersController
AnotherCustomersController
中使用提取的数据
function customersController($scope, $http) {
$http.get("http://www.w3schools.com//website/Customers_JSON.php")
.success(function(response) {$scope.names = response;});
}
function AnotherCustomersController($scope){
//What should I do here??
}
答案 0 :(得分:0)
您可以使用$rootscope在控制器之间共享数据,但我不认为这是最佳做法,因此我的解决方案包含角度服务的使用 - > plnkr
app.factory('CustomerService', function ($http) {
return {
fetchData: function () {
return $http.get('http://www.w3schools.com//website/Customers_JSON.php')
}
}
});
app.controller('customersController', function ($scope, CustomerService) {
CustomerService.fetchData().success(function (response) {
$scope.names = response;
});
});
app.controller('AnotherCustomersController', function ($scope, CustomerService) {
CustomerService.fetchData().success(function (response) {
$scope.names = response;
});
});
此外,我重构了您的代码,因此页面上仅使用一个应用。如果你想使用多个,你必须手动引导它们 - > Read More