とダイナミックテンプレート私はコンテキストパラメータに基づいて、動的に異なるテンプレートをロードするの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>
テンプレートを動的に読み込むことは問題なく動作します。 しかし、これらのテンプレートは、ロードが完了すると、「コールバック」関数を実行します。
私はそうのように追加することができる「オンロード」属性がある知っている:
<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>
...私は何らかの無限ループや何かを実行し、角度/ブラウザがクラッシュする(応答しなくなる)。コンテナのディレクティブに、それぞれのネストされたテンプレートに1つの "onLoad"関数を追加することなく、私が計画していることを達成する方法はありますか?
<search-results-container ng-include="views/result-list.html"></search-results-container>
使用結果:
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ディレクティブを使用してインスタンス化されています。私たちは1つのディレクティブ持っ
: