0

I have an angular application with 5000 array values in different variables. I need the same values across 3 controllers among the 5 controllers in whole project. Now I am using factory to keep the data across these. Is $rootScope a better efficient way or the current method is better.

Velu S Gautam
  • 822
  • 9
  • 18
  • 1
    You could also store them in local storage or fetch them each time from your data source. This question is too broad for a solitary, canonical answer – Phil Jun 17 '16 at 05:25
  • http://stackoverflow.com/questions/32761540/why-using-rootscope-with-functions-is-not-recommended please go through this link solution is addressed here and avoid using local storage and session storage as well – Vicky Kumar Jun 17 '16 at 05:27

2 Answers2

1

$rootScope is a parent of all scopes so values exposed there will be visible in all templates and controllers. Using the $rootScope is very easy as you can simply inject it into any controller and change values in this scope. It might be convenient but has all the problems of global variables.

Services are singletons that you can inject to any controller and expose their values in a controller's scope. Services, being singletons are still 'global' but you've got far better control over where those are used and exposed.

See the question - Global variables in AngularJS

Community
  • 1
  • 1
user2323308
  • 759
  • 5
  • 16
  • 34
0

$rootscope is not at all a good option. It is always preferable to use factories for data sharing.

Aravind
  • 2,290
  • 1
  • 11
  • 21