仅在输入大于3个字符时应用过滤器

时间:2017-03-28 05:53:20

标签: javascript angularjs angularjs-ng-repeat angular-filters

如果输入的过滤器只有大于3(或N)个字符时才能获得ng-repeat的结果?

我尝试了以下内容(过滤器:input.lenght> 3 ):

<input type="text" ng-model="input" placeholder="Search">

<li ng-repeat="detail in details | filter: input.lenght > 3">
  {{ detail.name }}
</li>

但它不起作用。我怎么能做我想做的事?问候朋友。

解决方案:

<input type="text" ng-model="input" ng-minlength="3" placeholder="Search">

1 个答案:

答案 0 :(得分:3)

对此过滤器使用三元运算

<li ng-repeat="detail in details | filter: (input.length > 3)? input : ''">

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.details = [{"name":"ssss"},{"name":"aaaa"},{"name":"cccc"}]
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <input type="text" ng-model="input" placeholder="Search">

<li ng-repeat="detail in details | filter: (input.length > 3)? input : ''">
  {{ detail.name }}
</li>
</div>