我有一个根据人名进行过滤的预输入。我的 person 对象有更多字段,而不仅仅是姓名(至少 {name, surname}
),并且我希望过滤器根据姓名和姓氏进行工作。
这是当前的预输入,基于 angular-ui-bootstrap
:
<input type="text" id="friendInput" placeholder="Friend's name"
typeahead="friend for friend in friendsList | filter:{name:$viewValue}"
typeahead-template-url="friendDropdown.html" ng-model="chosenFriend" />
我尝试过编辑 typeahead
属性的 filter
部分,但我想我还没有找到正确的方法。正确的方法是什么?
最佳答案
您可以在范围内声明一个过滤器并使用它
<input type="text" id="friendInput" placeholder="Friend's name"
typeahead="friend for friend in friendsList | filter:nameFilter"
typeahead-template-url="friendDropdown.html" ng-model="chosenFriend" />
并在你的 Controller 中声明
$scope.nameFilter = function(friend) {
var regex = new RegExp('' + $scope.chosenFriend, 'i');
return regex.test(friend.name) || regex.test(friend.surname); // test on both field
}
关于javascript - 来自多个字段的 Angular Typeahead 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26117120/