Sam Daniel Sam Daniel - 1 year ago 130
AngularJS Question

How to pass values from one controller to another controller

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?

Answer Source

There is 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.

.controller('myController1', ['myService', function (myService) {

.controller('myController2', ['myService', function (myService) {

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

  return service