Cybrix
Cybrix

Reputation: 3318

$rootScope vs top most ng-controller

I was wondering this:

Should we assign the commonly* used variables inside $rootScope or should we declare them in a top-most parent ng-controller?

Thank you

Upvotes: 0

Views: 192

Answers (1)

pkozlowski.opensource
pkozlowski.opensource

Reputation: 117370

This is a question very similar to the Global variables in AngularJS

Generally speaking you should try to avoid putting things on a $rootScope as it is AngularJS equivalent of a global window scope. Since the $rootScope can be injected everywhere (services, directives etc.) variables declared on the root scope a truly global.

If you've got a top-most, app-level controller and sticking variables in there does the trick for you I would favor this over polluting the $rootScope. As a general rule of thumb we should be using the most restrictive / lower-level scope.

Don't forget that a service might be an answer here.

Upvotes: 3

Related Questions