[英]Dynamically include a directive in another directive
I have a directive called table-div which renders the header and body of a table. 我有一个名为table-div的指令,该指令呈现表的标题和正文。 Each row within tbody could add some extra functionality or not.
tbody中的每一行都可以添加或不添加其他功能。 In case it has this ability then a custom directive should be included to print out some data and be linked to the scope of its parent directive.
如果具有此功能,则应包含一个自定义指令以打印出一些数据并将其链接到其父指令的范围。
The table-div directive: table-div指令:
<div>here is header</div>
<div ng-repeat="row in tbody">
<table-div-row data="row"></table-div-row>
<table-div-row data="row" directive="deletable"></table-div-row>
<table-div-row data="row" directive="editAvailable"></table-div-row>
</div>
My table-div-row directive looks along the lines of this 我的table-div-row指令看起来像这样
<div>
<p>some data here</p>
<div class="{{directive}}"></div>
</div>
And my editAvailable directive looks like this 我的editAvailable指令看起来像这样
<div>
<p> name: {{parentDirectiveScope.name}}</p>
<button>edit</button>
<button>save</button>
</div>
How would this be achieved ? 如何实现呢? Basically the problem is somehow linking that variable which holds the directive name and evaluate it somehow to actually display the directive.
基本上,问题在于以某种方式链接保存指令名称的变量并以某种方式对其进行评估以实际显示指令。 Is this possible ?
这可能吗 ?
Found out that I can use ng-include directive to include an template file. 发现我可以使用ng-include指令包含模板文件。 Basically my table-div-row would look like
基本上我的table-div-row看起来像
<div>
<p>some data here</p>
<ng-include src="{{directive}}"></ng-include>
</div>
the {{directive}} would be actually a url to a template. {{directive}}实际上是模板的网址。
And then my editAvailable "directive" would look like 然后我的editAvailable“指令”看起来像
<div ng-controller="EditAvailableCtrl">
<p> name: {{name}}</p>
<button>edit</button>
<button>save</button>
</div>
I have to create an EditAvailableCtrl which acts like a directive (meaning it has dom interactions as well). 我必须创建一个行为类似于指令的EditAvailableCtrl(这意味着它也具有dom交互)。
I couldn't find another way to this solution but it seems to be working properly and it allows me to dynamically load templates. 我找不到该解决方案的另一种方法,但它似乎工作正常,并且可以动态加载模板。 It is important to note that EditAvailableCtrl $scope will inherit from the parent scope so that gives you a lot of flexibility.
重要的是要注意,EditAvailableCtrl $ scope将继承自父作用域,因此具有很大的灵活性。
I am open to suggestions, maybe this is not a good practice but it does the work for me. 我乐于接受建议,也许这不是一个好习惯,但这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.