$ P $`pvent从创建隔离范围NG-include`。 AngularJS [英] Prevent `ng-include` from creating isolated scope. AngularJS

查看:221
本文介绍了$ P $`pvent从创建隔离范围NG-include`。 AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NG-包括在我的HTML多个地方的src 指令。每个 NG-包括正在为自身的隔离范围,这显然是造成了很多麻烦我。

I am using ng-include src directive at multiple places in my HTML. Each ng-include is creating an isolated scope for itself which apparently is causing a lot of trouble for me.

例如。

<div ng-controller="parent">
    <div ng-include src="'id1'">
    </div>
    <div ng-include src="'id2'">
    </div>
    <div ng-include src="'id2'">
    </div>
</div>

在上述的HTML,都得到4作用域。 1在父母其中,控制器定义和3得到通过在每个 NG-包括SRC 默认创建的。

In the html mentioned above, 4 scopes are getting created. 1 at parent where controller is defined and 3 are getting created by default at each ng-include src.

它是在所有可能prevent NG-包括从创建为自己一个孤立的范围是什么?如果这是不可能的,那么有没有解决办法呢?

Is it at all possible to prevent ng-include from creating an isolated scope for itself? If It is not possible then is there a workaround for it?

推荐答案

您需要创建一个自己的指令,可以加载指定的模板withoud隔离范围。

You need to create a your own directive that can load specified template withoud isolated scope.

HTML

<div ng-controller="parent">
    <div my-directive template-path="id1">
    </div>
    <div my-directive template-path="id2">
    </div>
    <div my-directive template-path="id2">
    </div>
</div>

指令

.directive('myDirective', function() {
  return {
      restrict: 'AE',
      templateUrl: function(ele, attrs) {
          return attrs.templatePath;
      }
  };
});

工作Plunkr

这篇关于$ P $`pvent从创建隔离范围NG-include`。 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆