1

Is it advisable to store data in $rootScope. I have a cordova app which uses sensor data which is coming every 100ms. For me to use that data in multiple controller I am using $rootScope.sensorData variable which is being refreshed every 100ms. Is it alright to use it this way? Is there a better way to do it?

Thank you

Pradeep Rajashekar
  • 565
  • 2
  • 9
  • 18
  • 1
    This has been already discussed here - http://stackoverflow.com/questions/35345446/best-practice-for-using-rootscope-in-an-angularjs-application – v1shnu Jul 29 '16 at 06:29

4 Answers4

2

You can store it in factory. In AngularJS factory is singleton, so only instance is created.

myApp.factory('SensorSrv', function SensorSrv() {
  var sensorData;
  return {
    setData: setData,
    getData: getData
  };

  function setData(data) {
    sensorData = data;
  }

  function getData() {
    return sensorData;
  }
});

You can also user local-storage if you want to persist the data.

Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
  • Using factory to serve data between multiple controller is the best way I know. But the data will be set every 100ms which will become a memory hogg. Implementing this effected performance even more than storing data in $rootScope. – Pradeep Rajashekar Jul 29 '16 at 10:39
1

I think this is not good idea to use $rootScope in entire code logic , There are lot of reasons behind that ... Instead of that you can create code login in services it is more flexible ... and also you can see this link

Best practice for using $rootscope in an Angularjs application?

Community
  • 1
  • 1
Gopinath Kaliappan
  • 6,929
  • 8
  • 37
  • 60
1

From the Official Docs:

$rootScope exists, but it can be used for evil

Scopes in Angular form a hierarchy, prototypally inheriting from a root scope at the top of the tree. Usually this can be ignored, since most views have a controller, and therefore a scope, of their own.

Occasionally there are pieces of data that you want to make global to the whole app. For these, you can inject $rootScope and set values on it like any other scope. Since the scopes inherit from the root scope, these values will be available to the expressions attached to directives like ng-show just like values on your local $scope.

Of course, global state sucks and you should use $rootScope sparingly, like you would (hopefully) use with global variables in any language. In particular, don't use it for code, only data. If you're tempted to put a function on $rootScope, it's almost always better to put it in a service that can be injected where it's needed, and more easily tested.

Conversely, don't create a service whose only purpose in life is to store and return bits of data.

--AngularJS Miscellaneous FAQ

georgeawg
  • 48,608
  • 13
  • 72
  • 95
0

I recommend using app.value app.value('test', 20); Because by using $rootScope you are exposing that value to all the services which might be a security threat. By using value you can make sure where do you want to use that variable according to your requirement.

Vikash Kumar
  • 1,712
  • 1
  • 11
  • 17