仅搜索typeahead中的特定对象参数

时间:2016-01-31 07:34:36

标签: angularjs

我正在使用angular typeahead搜索用户。我的数组包括empIdempNameimageUrlskills。在那里,我想只搜索empName& skills个参数。但目前结果也是过滤imageUrl。我只希望过滤empName& skills

以下是我的代码

<input type="text" placeholder="Search people here..." 
  ng-change="getMatchedUser();"  ng-model="selected" 
  data-typeahead="user as user.name for user in searchMembers | filter:$viewValue" 
  typeahead-on-select="onSelect($item, $model, $label)" 
  typeahead-template-url="customTemplate.html"  />

我的搜索数组位于

之下
$scope.searchMembers = [
                {
                 "empName":"abc",
                 "imageUrl":"default.jpg",
                 "skills": "java"
                },
                {
                 "empName":"pqr",
                 "imageUrl":"default.jpg",
                 "skills": "php"
                }
               ];

1 个答案:

答案 0 :(得分:0)

尝试编写自己的过滤功能。像

这样的东西
<input type="text" placeholder="Search people here..." ng-change="getMatchedUser();"  ng-model="selected" data-typeahead="user as user.name for user in getMatchingUsers($viewValue)" typeahead-wait-ms=100  typeahead-on-select="onSelect($item, $model, $label)" typeahead-template-url="customTemplate.html"  />

在控制器中,

$scope.getMatchingUsers=function($viewValue){
    var matchingUsers = [];
    for (var i=0; i< $scope.searchMembers.length; i++) {
      if (
        $scope.searchMembers[i].name.toLowerCase().indexOf($viewValue.toLowerCase()) != -1 ||
        $scope.searchMembers[i].skills.toLowerCase().indexOf($viewValue.toLowerCase()) != -1 ) {
        matchingUsers.push($scope.searchMembers[i]);      
      }
    }
    return matchingUsers;
}