Issue
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
Solution
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
Answered By - georgeawg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.