1

I have

1.) Controller1.js ( which has an array named Arr )

2.) Controller2.js

How should I pass Arr from Controller1.js to Controller2.js?

Abhishek Patil
  • 1,373
  • 3
  • 30
  • 62

1 Answers1

1

There are 2 ways to do it, best practice is to create service and store the value in it, and then inject it to both controllers. The angular service is singleton. You can read more here about services.

Second (and not good) way is to use $rootScope.

angular.module('myApp')
.controller('myController1', ['myService', function (myService) {
  //myService.array

}])
.controller('myController2', ['myService', function (myService) {
  //myService.array

}])
.service('myService', [function () {
  var service = {};
  service.array = [];

  return service
}])
E. Abrakov
  • 463
  • 2
  • 6