【问题标题】:Show/hide template URL's in angularjs在 angularjs 中显示/隐藏模板 URL
【发布时间】:2016-04-27 12:54:37
【问题描述】:

我在控制器中有一个动态数组,其中包含类似于下面的模板(html 文件)

 $scope.templates =
      [ { name: 'template1.html', url: 'template1.html'},
        { name: 'template2.html', url: 'template2.html'} ];

我有一个下拉菜单来选择模板。 我在 html 中使用 ng-include 来获取这些文件。 问题是: 当我加载了 template1.html 然后切换到 template2.html 时,我想隐藏 template1.html 数据并显示 template2.html。 下一次,当我选择template1.html时,我想隐藏template2.html数据并显示template1.html数据,而不是再次加载template1.html。

我在下面尝试过: html:

<div ng-controller="ExampleController">
  <select ng-model="template" ng-options="t.name for t in templates">
   <option value="">(blank)</option>
  </select>
  url of the template: <code>{{template.url}}</code>
  <hr/>
  <div class="slide-animate-container" ng-show="template.isHidden">
    <div  class="slide-animate" ng-include="template.url"></div>
  </div>
  <div class="slide-animate-container" ng-hide="template.isHidden">
    <div  class="slide-animate"></div>
  </div>
</div>

app.controller('ExampleController', ['$scope', function($scope) {
    $scope.templates =
      [ { name: 'template1.html', url: 'template1.html'},
        { name: 'template2.html', url: 'template2.html'} ];
    $scope.template = $scope.templates[0];
    $scope.$watch('template',function(newValue, oldValue) {
      $scope.templates[0].isHidden = true;//will do _.each to update
      $scope.templates[1].isHidden = true;
      var xyz = $scope.template;
      xyz.isHidden = false;
    });
  }]);

template1.html、template2.html可以是普通的html文件。

请提出建议。

https://plnkr.co/edit/TJKYbDrp1C1g4fQNHCi2?p=preview

【问题讨论】:

  • 你的代码不工作吗?
  • @MuliYulzary 隐藏成功,但未显示数据。我的计划是将数据保存在同一屏幕上,并在我们选择模板时将其隐藏并显示出来。
  • 你能在 plunker 中复制这个问题吗?
  • 这可能是多种情况:空模板、错误路径...您是否在控制台中遇到任何错误?
  • @Tuco 我复制了问题但无法保存。我正在尝试获取 plunker 链接。 plnkr.co/edit/TJKYbDrp1C1g4fQNHCi2?p=preview

标签: javascript html angularjs


【解决方案1】:

这一行:

<div class="slide-animate-container" ng-show="template.isHidden">

概率。意思是:

<div class="slide-animate-container" ng-include="template.url" ng-show="!template.isHidden">

如果isHidden 的值为false,您想显示 div。

【讨论】:

  • 我没有得到 template1.html 数据,因为我们没有包含它。我想在当前页面存储数据不想移动到另一个页面。
  • 你能澄清一下吗?
  • 我想在单屏中对 templateX.html 使用 show 和 hide。 plnkr.co/edit/TJKYbDrp1C1g4fQNHCi2?p=preview我的应用程序 html 文件非常大,每次我从下拉列表中选择文件时我们都无法重新加载页面
  • 您谈论的问题与您解决的问题不同。遗憾的是我必须去,但我会跟踪这个问题。
猜你喜欢
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
相关资源
最近更新 更多