1

Hi I just got tasked with maintaining a client written in AngjularJs and im not very familiar with Angjular but I just found this piece of code that I believe to be pure insanity..

There is a service that fetches an array of types from an API then attaches it to the rootScope

// just use a global, we'll need this array of types later $rootScope.opts = opts; 

The controller (which includes the service above)

 //this cant be right $scope.$rootScope = $rootScope; $scope.setCategory = (type) => { // trust that the service has set the options into $rootScope const option = $scope.$rootScope.opts.find( typeOption => typeOption.name === type); return option; } 

I would think it better to store the "opts" array in the service and just invoke it from the controller when needed ?

But before I nuke this code I must know if there any sense in doing it this way

4
  • 1
    Yeah for sure. You don’t assign $rootScope to the $scope. It would just be $rootScope.opts. And secondly the better option would be to use a service to get the opts and inject it into your controller. You are absolutely right. You don’t need $rootScope at all. Nuke that trash! Haha Commented Dec 8, 2020 at 15:07
  • Thanks for the sanity check, will definitely do that! Commented Dec 9, 2020 at 9:21
  • No problem. Sounds like you might have your work cut out for you. Good luck Commented Dec 9, 2020 at 11:37
  • yeah I wonder no more why im the 3rd dev to take over this project in 2020 Commented Dec 10, 2020 at 9:05

1 Answer 1

1

Check to see if the template associated with the controller uses $rootScope in its HTML. This is a very unstructured design and there are better ways to do it. Also a bit redundant because $scope already has a property named $root which points to the $rootScope.

For more information, see

Sign up to request clarification or add additional context in comments.

1 Comment

And this component ng-includes no less than 10 other templates, am I right in assuming that this $scope.$rootScope might be referenced in their templates / controllers as well ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.