简体   繁体   English

Angular.js $ scope和dom元素

[英]Angular.js $scope and dom element

How does angular keep track of which $scope is related to which element? 角度如何跟踪哪个$范围与哪个元素相关? I have a few theories: 我有一些理论:

  1. angular element keeps scope object as property angular元素将范围对象保持为属性
  2. angular saves each scope in cache with the relationship with element angular使用与元素的关系将每个范围保存在缓存中
  3. angular searches each ng-scope, and magically find element 角度搜索每个范围,并神奇地找到元素

For example, I have an element with $scope, and I want to change dom hierarchy of this element (moving to outside of the outer controller). 例如,我有一个带有$ scope的元素,我想改变这个元素的dom层次结构(移动到外部控制器之外)。 How is scope affected by this action? 范围如何影响此操作?

If you have an angular app, and your HTML looks something like this: 如果你有一个角度应用程序,你的HTML看起来像这样:

<div ng-controller="OuterController">
    ...
    <div ng-controller="InnerController">
        <div id="wrapper">
            <span>An Element</span>
        </div>
    </div>
</div>

And you move the <span> out of the #wrapper div, it's still within the context of both OuterController and InnerController . 然后你将<span>移出#wrapper div,它仍然在OuterControllerInnerController的上下文中。 However, if you move it up to where the ... is, only the OuterController 's context applies. 但是,如果将其移动到...所在的位置,则只应用OuterController的上下文。

Each controller has its own context, and that context extends down into all child-elements recursively, even if a new controller is present, at which point, both controllers are in-scope. 每个控制器都有自己的上下文,并且该上下文递归地向下扩展到所有子元素,即使存在新的控制器,此时两个控制器都在范围内。

This is true whether the element is moved dynamically with Javascript or otherwise. 无论是使用Javascript还是以其他方式动态移动元素,都是如此。 Angular keeps track of most context internally. Angular内部跟踪大多数上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM