Reputation: 8267
I have a controller where the $scope object has been used to store methods and values that are only used locally within the same controller. There is a lot of this going on:
$scope.foo = 'something';
$scope.bar = 'something else';
... and so on. None of these values are used within the view. My question is does polluting the $scope object affect performance? Is it a good idea to clean this up so only values and methods needed for the view are contained in the $scope object?
Upvotes: 4
Views: 545
Reputation: 136184
Yes, Polluting $scope
does affect performance, but its depends your scope has multiple watchers which are frequently changing then that will create a more overhead cost. Refer this answer which has covered same point
For avoiding this situation I'd suggest you to do good re-factoring of code
Handle all the logic in controller whenever required otherwise do separate a logic by making good use of each component.
service
/factory
/provider
which is used in multiple place, so that it would be sharable.constant
/value
$scope
it self then move that logic to common controller. When required you could inject in your current controller scope using $controller
injectorAlso refer Understanding Of Scope for clear understanding of use of scope
Upvotes: 4