【问题标题】:AngularJS dynamic template with ng-include and separate template directives具有 ng-include 和单独模板指令的 AngularJS 动态模板
【发布时间】:2017-07-22 07:46:21
【问题描述】:

我想创建一个基于上下文参数动态加载不同模板的 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());
    }
  }
}

我的“search-results-container.html”文件:

<div ng-include="templates[0].url"></div>

我的“search-results-table-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”填充我原来的“search-results-container”指令,这必须使用开关(或类似技术)来区分模板文件并运行具体功能取决于当前活动的功能 - 我想阻止它,因为它不干净。

我想为每个模板设置单独的指令,例如:

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 */
          }
        }
    }
});

如果我这样做,我会相应地更改我的“search-results-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>

...我遇到某种无限循环或其他情况,Angular / 浏览器崩溃(变得无响应)。有没有办法实现我的计划,而无需用大量“onLoad”函数填充容器的指令,每个嵌套模板一个?

【问题讨论】:

    标签: angularjs dynamic angularjs-directive angularjs-ng-include angularjs-templates


    【解决方案1】:

    我们最终做的是:

    我们有一个指令:

    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>
    

    使用的result-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-click 指令:

    <button ng-click="toggleViewMode()">Toggle View</button>
    

    这个按钮触发了 searchResultsContainer 的 toggleView 方法,在上面的指令中描述,移动模板数组的元素:

    scope.toggleView = function() {
        scope.templates.push(scope.templates.shift());
    }
    

    ng-switch 指令监听模板数组第一个元素的“viewmode”属性的变化:

    ng-switch on="templates[0].viewmode" 
    

    这种变化发生在单击按钮时,从而完全改变了数组中的第一个元素,这自然也会导致“viewmode”属性的变化,由ng-switch监控。

    嵌套的 ng-switch-default 和 ng-switch-when 指令对更改做出反应并显示相应的元素,该元素在“ng-switch-when”中设置了适当的值。

    ng-switch 的三个子项各自使用一个单独的指令,这里是其中之一:

    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-include 指令中的高逗号,这是 ng-include 指令 (more info on ng-include) 所要求的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 2020-03-01
      • 2016-03-09
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多