Sam Daniel Sam Daniel - 4 months ago 16x
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?


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