Search code examples
angularjsangularjs-rootscope

Is setting the value of rootScope.angular a code smell?


I'm looking at this code that someone else wrote. This looks to me like a bad code smell. What could be a valid reason to copy a reference to angular under the $rootScope?

    $rootScope._ = $window._;
    $rootScope.angular = $window.angular;

Solution

  • As a rule of thumb, it's generally bad to assign anything to $rootScope in AngularJS. It's like assigning a global variable in most languages -- when you find yourself doing it, you should probably stop to think if there's another way to solve your problem. They tend to make code messy and impossible to follow.

    This on the other hand... this is just extra weird, and is most definitely a bad code smell. I can't see a single valid reason why you would throw an already globally accessible variable on $rootScope, especially throwing angular itself onto $rootScope.