Javascript 搜索功能不支持';使用Angular.js输入部分名称时不起作用

Javascript 搜索功能不支持';使用Angular.js输入部分名称时不起作用,javascript,html,angularjs,Javascript,Html,Angularjs,我使用angular.js从我的表中搜索餐厅名称。假设有许多“餐厅名称”,如果用户键入任何名称的第一个字母,则在键入2/3个字母后,其过滤不符合要求 这是我的密码: <div class="input-group" style="margin-bottom:10px; width:300px;"> <input class="form-control" placeholder="Type Restaurant

我使用angular.js从我的表中搜索餐厅名称。假设有许多“餐厅名称”,如果用户键入任何名称的第一个字母,则在键入2/3个字母后,其过滤不符合要求

这是我的密码:

<div class="input-group" style="margin-bottom:10px; width:300px;">
<input class="form-control" placeholder="Type Restaurant Name" name="q" type="text" ng-model="searchProduct.rest_name">
</div>
<table class="table table-bordered table-striped table-hover" id="dataTable" >
<thead>
<tr>
<th>Sl. No</th>
<th>Restaurant Name</th>
</tr>
</thead>
<tbody id="detailsstockid">
   <tr dir-paginate="cus in ($parent.labelResults=(listOfCustomerData  | filter:searchProduct.rest_name:startsWith | orderBy:'rest_name')) | itemsPerPage:5 track by $index" current-page="currentPage">
  <td>{{itemsPerPage *(currentPage-1)+$index+1}}</td>
   <td>{{cus.rest_name}}</td>
</tr>
</tbody>
</table>
我有很多餐厅的名字,如Anjum,A&p中餐快车,Bookers BBQ&Crab Shack,Butcher And The Baker,Cactus Club Stephen Avenue,Cactus Club-Macleod Trail


当用户在搜索框内仅键入
a
时,以
a
开头的名称应进行过滤,但不会出现这种情况。所有数据显示在2/3型字母后,过滤过程正在工作。这里我需要当用户键入第一个字母时,与该搜索字母相关的名称将被过滤。

给输入一个
最小长度

i、 e,
ng minlength=1

<input class="form-control" placeholder="Type Restaurant Name" name="q" 
type="text" ng-model="searchProduct.rest_name" ng-minlength="1">

下面的链接可以帮助您解决问题。 这似乎和你想要达到的目标是一样的。

在上面的链接中,他们提到了如何应用自定义过滤器使angularjs中的单个字符(第一个字母)调情

因为在angularjs中发生单字母过滤,但它会过滤 喜欢角色“S”
它也占用了“三明治”和“面包”,尽管“面包”是无效项。

Angular的过滤器表达式不执行以
开头的
操作。它没有一个
包含的
。对于您提供的餐厅名称示例,在筛选器中键入“a”不会筛选出任何名称。最简单的解决方案是在控制器中添加一个函数:

var-app=angular.module('app',[]);
应用程序控制器('myCtrl',函数($scope){
$scope.listOfCustomerData=[{
休息时间名称:“Anjum”
}, {
其余名称:“A&P中国食品快车”
}, {
休息名称:“布克烧烤蟹屋”
}, {
其他名称:“屠夫和面包师”
}, {
休息名称:“仙人掌俱乐部斯蒂芬大道”
}, {
休息名称:“仙人掌俱乐部-麦克劳德小径”
}];
$scope.startsWith=函数(实际、预期){
var lowerStr=(实际值+“”)。toLowerCase();
返回lowerStr.indexOf(应为.toLowerCase())==0;
}
});

序号
餐厅名称
{{$index+1}}
{{cus.rest_name}

我想你要找的是打字。有很多解决方案(我前一段时间测试的是Bootstrap库:。@FDavidov:我需要在搜索框中键入字母后从表中筛选一些数据。这里我使用的是Angular.js。我不确定它是否有效。你有任何演示代码吗?抱歉,没有演示代码。这是很久以前的事了。我建议你在google上搜索Bootstrap typeahead。你可以“我将看到大量的演示和示例代码。@TahTatsumoto:您能通过在此处键入此特定问题的代码来共享您的代码吗?让我来实现。您的代码工作正常。但我在文件中实现了相同的代码,它没有按预期工作。当我选中console message
lowerStr.indexOf(expected.toLowerCase())时===0
,在您的代码中,当用户键入内容时,它总是返回
false
,但在我的代码中,它第一次打印
false
,然后是
true
,并在用户输入任何字母时重复。请在代码中不使用指令的情况下尝试。如果它运行两次,则会有其他内容触发它。您的意思是我将运行它不带
启动带
或什么?
<input class="form-control" placeholder="Type Restaurant Name" name="q" 
type="text" ng-model="searchProduct.rest_name" ng-minlength="1">