为什么使用ng-include外层会多一个作用域?

为什么会有一个id=7的scope?有什么用?

图片描述

<body ng-app="scopeExample">
    <div class="show-scope-demo">
      <div ng-controller="GreetController">
        Hello {{name}}!
        <div ng-controller="ListController">
          <ol>
            <li ng-repeat="name in names">{{name}} from {{department}}</li>
          </ol>
          <input ng-model="name">{{name}}
          <div ng-include src="'child.html'">

          </div>
        </div>
      </div>
    </div>
  </body>
  

child.html

<div ng-controller="ChildController">
  <input ng-model="name">
</div>  


阅读 2.3k
1 个回答

我写过类angular的框架,一开始也提供了include功能,我的做法也是在外面套一个div,原因是什么呢,因为被include的 代码片段,不一定是一个被包裹起来的dom元素,很有可能是一组并排的div

例如:
child.html

<div ng-controller="Child1Controller">
  <input ng-model="name">
</div>
<div ng-controller="Child2Controller">
  <input ng-model="name">
</div>

所以针对这种情况,会在罪外层多包裹一层div再append到目标位置。我估计angular也是出于类似的考虑吧。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题