我想要创建一个div,它根据上下文参数动态加载不同的模板:
我的“搜索结果-容器”指令:
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());
}
}
}我的“搜索-结果-容器.file”文件:
<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 */
}
}
}
});如果我这样做,并相应地将“搜索-结果-table-view.html”更改为:
<search-results-table-view>
<table>
<tr ng-repeat="item in ctrl.items">
<td>{{ item.title }}</td>
<tr>
</table>
</search-results-table-view>...I遇到某种无限循环或什么东西,导致浏览器崩溃(变得没有响应)。有没有一种方法可以实现我所计划的,而不用为每个嵌套模板填充大量"onLoad“函数的容器指令呢?
发布于 2017-04-04 09:11:45
我们最后做的是:
我们有一个指令:
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());
}
}
}通过使用ng-include指令实例化此指令:
<search-results-container ng-include="views/result-list.html"></search-results-container>使用的结果-list.html文件有以下段落:
<div ng-switch on="templates[0].viewmode">
<search-results-list-view ng-switch-default></gs-search-results-list-view>
<search-results-grid-view ng-switch-when="grid"></gs-search-results-grid-view>
<search-results-table-view ng-switch-when="table"></gs-search-results-table-view>
</div>它还有一个按钮可以在视图之间切换,使用一个简单的ng单击指令:
<button ng-click="toggleViewMode()">Toggle View</button>此按钮触发搜索结果容器的toggleView方法(在上面的指令中描述),将模板数组的元素移动到:
scope.toggleView = function() {
scope.templates.push(scope.templates.shift());
}ng开关指令侦听模板数组第一个元素的"viewmode“属性中的更改:
ng-switch on="templates[0].viewmode" 这样的更改会在单击按钮时发生,从而完全改变数组中的第一个元素,这自然也会导致由ng开关监视的"viewmode“属性的更改。
嵌套的ng开关-默认值和ng-开关-当指令对更改作出反应,并显示相应的元素,该元素在“ng-开关-时间”中设置了适当的值。
ng交换机的三个子程序分别使用一个单独的指令,下面是其中一个:
app.directive("searchResultsListView", function() {
return {
restrict: "E",
require: "^searchResultsContainer",
template: "<div ng-include=\"'views/list-view.html'\"></div>",
controller: function($scope) {
/* $scope stuff goes here */
},
link: function(scope, element, attrs, searchResultsCtrl) {
/* link stuff goes here */
}
}
});注意非常重要的转义引号,然后是模板的ng中的逗号--包括指令,这是ng-include指令(有关ng的更多信息-包括)所要求的。
https://stackoverflow.com/questions/42557877
复制相似问题