防止ng-include创建隔离范围AngularJS

【字号: 日期:2024-04-10浏览:47作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决防止ng-include创建隔离范围AngularJS?

您需要创建自己的指令,该指令可以在没有隔离范围的情况下加载指定的模板。

<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; } };});解决方法

我ng-include src在HTML的多个位置使用指令。每个人ng-include都为自己创建了一个孤立的范围,这显然给我带来了很多麻烦。

例如。

<div ng-controller='parent'> <div ng-include src='https://www.6hehe.com/wenda/’id1’'> </div> <div ng-include src='https://www.6hehe.com/wenda/’id2’'> </div> <div ng-include src='https://www.6hehe.com/wenda/’id2’'> </div></div>

在上面提到的html中,正在创建4个作用域。controller默认情况下,在定义的父级中创建1个,默认情况下创建3个ng-include src。

是否有可能阻止ng-include自身创建隔离范围?如果不可能,那么是否有解决方法?

相关文章: