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.

2
  • 1
    Why not use MainController? Commented May 1, 2014 at 8:49
  • Stupid of me not to include that option. I guess my question is more whether or not service or $scope/$rootScope is better semantically. Commented May 1, 2014 at 8:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.