$ 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;
}
};
});