AngularJS,使用ng-repeat显示/隐藏行表

时间:2018-11-28 01:11:26

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

对不起,我是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>

1 个答案:

答案 0 :(得分:0)

是否可能必须为isShow中的每个name添加属性names? 或为每个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>