我想创造出动态加载不同的模板一个div,基于上下文参数:AngularJS动态模板NG-包括独立的模板指令
我的“搜索结果的容器”指令:
app.directive("searchResultsContainer", function() {
restrict: "E",
templateUrl: "search-results-container.html",
controller: function($scope) {
$scope.templates = [
{ viewmode: "list", url: "search-results-list-view.html" },
{ viewmode: "grid", url: "search-results-grid-view.html" },
{ viewmode: "table", url: "search-results-table-view.html" }
]
},
link: function(scope) {
scope.toggleView = function() {
scope.templates.push(scope.templates.shift());
}
}
}
我的 “搜索结果的container.html” 文件:
<div ng-include="templates[0].url"></div>
我的 “搜索结果的表view.html” 文件:
<table>
<tr ng-repeat="item in ctrl.items">
<td>{{ item.title }}</td>
<tr>
</table>
加载模板动态工作没有问题。 但是,我希望这些模板在加载完成后运行一些“回调”功能。
我知道有“的onload”属性,我可以添加到这么喜欢:
<div ng-include="templates[0].url" onload="onLoadFunction()"></div>
这意味着,我需要填充我原来的“搜索结果的容器”指令与“ onLoadFunction“,它必须使用开关(或类似技术)来区分模板文件并根据当前活动的函数运行特定的函数 - 我想阻止它,因为它不干净。
我想有不同的配置指令为每个模板,如:
app.directive("searchResultsTableView", function() {
return {
restrict: "E",
templateUrl: "search-results-table-view.html",
controller: function($scope) {
...
},
link: function(scope) {
scope.someOnLoadFunction = function() {
/* Stuff to execute when template has loaded */
}
}
}
});
如果我这样做,我改变我的“搜索结果的表view.html”进行相应的这样:
<search-results-table-view>
<table>
<tr ng-repeat="item in ctrl.items">
<td>{{ item.title }}</td>
<tr>
</table>
</search-results-table-view>
......我遇到了某种无限循环或者什么,Angular /浏览器崩溃(变得无法响应)。有没有一种方法可以实现我的计划,而不用用大量的“onLoad”函数来填充容器的指令,每个嵌套模板都有一个函数?