-1

How can I use the fetched data in customersController in 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??
}     

Full Details Here

Blackhole
  • 20,129
  • 7
  • 70
  • 68
Learner
  • 13
  • 1

1 Answers1

0

You can share data between controllers using $rootscope but I don't think this is a best practice so my solution contain usage of angular service -> 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;
  });

});

Additionally i have refactor your code so only one app is used on page. If you want to use more than one you have to bootstrap them manually -> Read More

Community
  • 1
  • 1
Teq1
  • 631
  • 1
  • 6
  • 20