0

I'd like to know the recommended way of maintaining app state in an Angular. For example I am building a game and I'd like to display/update the score across the whole app. Is it semantics better to use $rootScope, MainController $scope, or a service.

I currently have this structure:

<html ng-app="MyApp">
<body ng-controller="MainController">
        <ng-view></ng-view>
</body>
</html>

So the main controller is always present and Angular's routing takes care of the specific sections.

I understand I could assign score data to $rootScope and update/read that. But is it better semantically to use a service? Such as scoreService and inject this into each controller/directive that needs to update/read from it?

I've read putting $rootScope is convenient but makes testing harder. However I've also read it's a bad idea to have services that simply set and get data.

Alex B
  • 1,009
  • 3
  • 18
  • 26

0 Answers0