所以我有自己的自动完成功能,我使用ng重复来显示建议,使其像真正的自动完成功能一样,我使用ng click来更改文本框的值,但是当我单击或文本框的值已经与建议相同时,这个建议并没有消失。如果在此代码中如何使用ng?这是我的代码
对于HTML:
<input type="text" class="form-control" data-ng-model="add.email" id="exampleInputEmail2" placeholder="Email" autocomplete="off"/>
<div href="#" ng-repeat="x in cobas | filter:add.email" ng-click="autocomplete(x.name)" ng-if="isDisplayed">
<div class="media-body" ng-if="add.email.length > 0">
<h5 class="list-group-item media">{{x.name}}
</div>
</div>
和我的控制器:
$scope.cobas = [
{name:'John', age:25, gender:'boy'},
{name:'Jessie', age:30, gender:'girl'},
{name:'Johanna', age:28, gender:'girl'},
{name:'Joy', age:15, gender:'girl'},
{name:'Mary', age:28, gender:'girl'},
{name:'Peter', age:95, gender:'boy'},
{name:'Sebastian', age:50, gender:'boy'},
{name:'Erika', age:27, gender:'girl'},
{name:'Patrick', age:40, gender:'boy'},
{name:'Samantha', age:60, gender:'girl'}
];
$scope.autocomplete = function (completeText){
$scope.add.email = completeText;
};
最佳答案
只需使用额外的检查,看看输入中的值是否等于下拉列表中的值。ng-if="add.email.length > 0 && add.email != x.name"