我想在输入3个字符后开始搜索?

时间:2017-07-19 10:50:14

标签: javascript angular datalist

这是我的html代码,我想在进入3个字符后开始搜索值: -

<input id="restaurants" list="lstRestaurants" type="text" class="form-control form-input input-search input-section clearable" placeholder="Name of Restaurant" [(ngModel)]="searchModel.restaurant" name="restaurant" #restaurant="ngModel">
<datalist id="lstRestaurants">
   <option *ngFor="let restaurant of allRestaurants" value="{{restaurant.name}}"></option>
</datalist>

2 个答案:

答案 0 :(得分:0)

你应该在searchModel.restaurant上设置观察:

$scope.$watch('searchModel.restaurant', function(newValue){
 if(newValue.length === 3){
   //search on your datalist
   alert("Search by newValue");
 }});

答案 1 :(得分:-1)

希望这会让你更接近你的答案。

$(document).on('keyup', '#restaurants', function() {
var myLength = $("#restaurants").val().length;
if (myLength > 2) {
      alert("Do search now")
    };
alert("each letter you type this pops up");
});