gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:09:01 25 4
gpt4 key购买 nike

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

<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>

谢谢

最佳答案

只需添加一个 ng-if

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

演示

<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>

关于javascript - 输入为空时如何隐藏所有名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com