【问题标题】:Angularjs, show/hide row's table with ng-repeatAngularjs,使用 ng-repeat 显示/隐藏行表
【发布时间】:2019-04-29 20:09:15
【问题描述】:

对不起,我是 ng-repeat 的新手。如何显示/隐藏使用 ng-repeat 的行表?如果下拉值为 1,则显示最底行。

    var i ;
    $scope.names = [];
    $scope.tmp = [];
    for(i=0;i<=10;i++){
        $scope.tmp[i] = i;
        $scope.names[i] = "name "+ i;
    }
    $scope.isShow = true

html

<select>
        <option ng-repeat="x in tmp">{{x}}</option>
</select>
 <table>
   <tr ng-show='isShow' ng-repeat="name in names">
     <td>{{name}}</td>
   </tr>
 </table>

【问题讨论】:

  • 不清楚您要在这里完成什么,但一般来说,如果您要生成多个需要彼此独立显示或隐藏的项目,则每个项目都必须跟踪它自己的可见性,这意味着单个 isShow 属性是不够的。

标签: javascript angularjs html-table angularjs-ng-repeat


【解决方案1】:

您是否必须为names 中的每个name 添加属性isShow? 或者为每个name 创建具有可见状态的数组。

angular.module('app', [])
  .directive('appDir', appDir);

angular.bootstrap(
  document.getElementById('root'), ['app']
);

function appDir() {
  return {
    template: `
       <table>
          <tr
              ng-repeat="name in names"
              ng-show="name.isShow"
          >
              <td>
                  {{name.title}}
              </td>
          </tr>
       </table>
       <select
          ng-model="selectedName"
          ng-options="x as x for x in tmp"
          ng-change="hiddenName()"
       >
    `,
    link: appDirLink
  }
}

function appDirLink($scope) {
  $scope.names = [];
  $scope.tmp = [];
  $scope.hiddenName = hiddenName;
  for (var i = 0; i < 10; i++) {
    $scope.names[i] = {
      id: i,
      title: 'name_' + i,
      isShow: true
    };
    $scope.tmp[i] = i;
  }

  function hiddenName() {
     $scope.names.map((name, index) => {
        name.isShow = (index < $scope.selectedName) ? true : false;
     });
  }

}
<div id="root">
    <app-dir></app-dir>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>

【讨论】:

  • 感谢您的回复。我的想法是,如果下拉列表为 2,则显示 2 行(从底部开始),并隐藏上面的 8 行。
  • 我改变了我的例子。见hiddenName函数
  • 什么是 $scope.names.map((name, index) => { name.isShow = (index
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 2020-02-25
  • 2014-10-15
相关资源
最近更新 更多