AngularJS如何设置范围$ id的值?似乎没有增量。

时间:2015-05-25 08:54:56

标签: javascript angularjs angularjs-scope

我正在尝试进一步研究AngularJS中的范围层次结构。最近,我一直在密切关注范围。$ id编号 - 我注意到应用于范围$id的值不是以增量方式应用的。例如:

<html ng-app="app">
    <body ng-controller="appCtrl">
         <!-- ngInclude: 'notifications.tpl.html' -->
         <!-- ngInclude: 'otherTpl.tpl.html' -->
          <div ng-include="someView.tpl.html">
          <ui-view></ui-view>
     </body>
</html>

这个树镜像我的DOM,其中html元素的范围$id为1,body的{​​{1}}为2,{{1范围$id为6,这似乎没有多大意义。

这些ng-include数字如何确定?它有意义吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

id算法是:

function HashMap(array, isolatedUid) {
  if (isolatedUid) {
    /* Initialize uid to zero for the rootscope */
    var uid = 0;
    this.nextUid = function() {
      return ++uid;
    /* Increment the id */
    };
  }
  forEach(array, this.put, this);
}

范围算法是:

        if (isolate) {
          child = new Scope();
          /* Increment when a directive which uses isolate scope is created */
          child.$root = this.$root;
        } else {
          // Only create a child scope class if somebody asks for one,
          // but cache it to allow the VM to optimize lookups.
          if (!this.$$ChildScope) {
            this.$$ChildScope = createChildScopeClass(this);
          }
          /* Increment when a directive which uses child scope is created */
          child = new this.$$ChildScope();
        }
  

如果您发现您的代码现在抛出$ compile:multidir错误,请检查您是否在尝试同时请求隔离和非隔离范围的同一元素上没有指令并修复代码。< / p>      

如果您的代码依赖于此行为(非隔离指令需要从隔离范围内访问状态),请更改isolate指令以使用范围本地明确地传递这些。

     

scope。$ new()接受一个参数 - 一个布尔值,指示是否应该隔离新创建的子作用域(不是原型继承当前作用域)。以前第一个参数是对控制器构造函数的引用,但由于范围/控制器分离,控制器应该通过$ controller服务实例化。

<强>参考