2017-01-28 5 views
0

Я пытаюсь добавить autocomlete по typeahead к моему проекту, но теперь он отображается в раскрывающемся списке [object Object]. Где я ошибаюсь?Typeahead с AngularJS

$scope.getUsers = function() { 
     $scope.searchRequest = "/listaccounts.php?name=" + $scope.asyncSelected; 
     console.log($scope.searchRequest); 
     return $http.get($scope.searchRequest).then(function (response) { 
      $scope.searchResults = response.data; 
      return $scope.searchResults.records; 
     }); 
    }; 

<input type="text" ng-model="asyncSelected" ng-change="asyncSelected = asyncSelected.toLowerCase()" name="user" class="form-control" aria-describedby="basic-addon1" autocomplete="off" uib-typeahead="name for name in getUsers($viewValue)"> 

JSON выглядит следующим образом:

{"records":[{"name":"q2q23w"},{"name":"qantheman"},{"name":"qee"},{"name":"qit"},{"name":"qiwi"}]} 

мне нужно показать в выпадающем списке только имена.

+0

Это должно работать с пользователем 'uib-typeahead =" как user.name для пользователя в getUsers ($ viewValue) "' – choz

ответ

1

getUsers() возвращает массив объектов и машинописный, кажется, есть проблема с этим. Либо @hadiJz решение будет работать (я не знаю), или вы можете вернуть список строк из getUsers() путем перехода к:

return $scope.searchResults.records.map(function(record) { return record.name; })); 
0

Попробуйте

uib-typeahead="name.name as name.name for name in getUsers($viewValue)"> 
0

Вы можете сделать,

<input name="records" id="records" type="text" placeholder="enter a record" ng-model="selected" typeahead="record.name for record in records | filter:$viewValue | limitTo:8" class="form-control"> 

DEMO