Difference between $scope and $rootScope

后端 未结 9 1753
孤城傲影
孤城傲影 2020-11-28 02:33

Can anyone explain the difference between $scope and $rootScope?

I think

$scope:

We can get ng-model properties in particular cont

9条回答
  •  -上瘾入骨i
    2020-11-28 03:00

    In other way we can look at this; $rootScope is global while $scope is local. When Controller is assigned to a page, so a $scope variable can be use here because it binds to this controller. But when we want to share its value across to other controllers or services, then $rootScope is being used (**there are alternative ways, we can share values across but in this case we want to use $rootScope).

    Your second question about how you define those two words are correct.

    Lastly a bit off track, please use $rootScope with care. Similar to the way you use global variables, can be a pain to debug and you may accidentally change the global variable somewhere inside a timer or something which makes your reading incorrect.

提交回复
热议问题