Does polluting the $scope object affect performance?

狂风中的少年 提交于 2019-12-11 02:48:30

问题


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?


回答1:


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.

  1. Move common method(logic) to service/factory/provider which is used in multiple place, so that it would be sharable.
  2. If some value are fixed, they are not gonna change then move them to constant/value
  3. Whenever you feel like you have same logic which needs to be keep in $scope it self then move that logic to common controller. When required you could inject in your current controller scope using $controller injector

Also refer Understanding Of Scope for clear understanding of use of scope



来源:https://stackoverflow.com/questions/30221978/does-polluting-the-scope-object-affect-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!