输入为空时如何隐藏所有名称?

时间:2022-11-11 08:12:58

In the following code, when input is empty, all the names are shown. I want no name when the input field is empty.

在以下代码中,当输入为空时,将显示所有名称。当输入字段为空时,我不需要名称。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
<input ng-model="search.$"></label> <br>
<ul>
  <li ng-repeat="x in names | filter : search">
    {{ x }}
  </li>
</ul>
</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

Thanks

谢谢

3 个解决方案

#1


3  

Just add a ng-if

只需添加ng-if

 <ul ng-if="search.$">

DEMO

DEMO

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
<input ng-model="search.$"></label> <br>
<ul ng-if="search.$">
  <li ng-repeat="x in names | filter : search">
    {{ x }}
  </li>
</ul>
</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

#2


1  

Use ng-if

使用ng-if

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
<input ng-model="search.$"></label> <br>
<ul>
  <li ng-if="search.$" ng-repeat="x in names | filter : search">
    {{ x }}
  </li>
</ul>
</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

#3


1  

Try This Code it will help you

试试这个代码它会帮助你

<script https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
  <input ng-model="search"></input> <br>
  <ul>
    <li ng-repeat="x in names | filter : search">
      {{ x }}
    </li>
  </ul>
</div>

<script>
  angular.module('myApp', []).controller('namesCtrl', function($scope) {

    if ($scope.$$childHead.search == "") {
      $scope.names = [];
    } else {
      $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
      ];
    }
  });
</script>

<!-- end snippet -->

#1


3  

Just add a ng-if

只需添加ng-if

 <ul ng-if="search.$">

DEMO

DEMO

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
<input ng-model="search.$"></label> <br>
<ul ng-if="search.$">
  <li ng-repeat="x in names | filter : search">
    {{ x }}
  </li>
</ul>
</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

#2


1  

Use ng-if

使用ng-if

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
<input ng-model="search.$"></label> <br>
<ul>
  <li ng-if="search.$" ng-repeat="x in names | filter : search">
    {{ x }}
  </li>
</ul>
</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

#3


1  

Try This Code it will help you

试试这个代码它会帮助你

<script https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
  <input ng-model="search"></input> <br>
  <ul>
    <li ng-repeat="x in names | filter : search">
      {{ x }}
    </li>
  </ul>
</div>

<script>
  angular.module('myApp', []).controller('namesCtrl', function($scope) {

    if ($scope.$$childHead.search == "") {
      $scope.names = [];
    } else {
      $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
      ];
    }
  });
</script>

<!-- end snippet -->