> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/angularjs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/angularjs/scope.md).

# $scope对象

## 什么是 scope

* scope是一个指向应用model的object，也是表达式的执行上下文。
* scope被放置于一个类似应用的DOM结构的层次结构中。

## scope的特性

* scope提供$watch API,⽤用于监测model的变化。
* scope提供$apply，在"Angular realm"（controller、server、angular event handler）之外，从系统到视图传播任何model的变化。
* scope可以在提供到被共享的model属性的访问的时候，被嵌入到独立的应⽤用组件中。scope通过（原型），从parent scope中继承属性。

## scope生命周期

1. 用户请求应用起始页
2. angular 被加载，查找ng-app指令
3. Angular 遍历模版，查找指令
4. controller被启用，$scope被注入进来
5. 在模版link过程中，指令在scope中注册$watch。这些watch将会被⽤用作向DOM传播model的值。
6. 当在controller中做同步的⼯工作时angular API 已经隐式地做了$apply操作
7. 在$apply的结尾，angular会在root scope执行一个$digest周期，这将会传播到所有child scope中。在$digest周期中，所有注册了$watch的表达式或者function都会被检查，判断model是否发生了改变，如果改变发生了，那么对应的$watch监听器将会被调用。
8. 当child scope不再是必须的时候，child scope的产⽣生者有责任通过scope.$destroy() API销毁它们（child scope）。这将会停止$digest的调用传播传播到child scope中，让被child scope model使用的内存可以被gc回收。
