Каково правильное значение для uib-typeahead для асинхронного API, который возвращает массив строк? - PullRequest
0 голосов
/ 27 апреля 2018

Какое правильное значение для uib-typeahead, vm.searchCompanies возвращает массив строк вроде ["", "aaa"] У меня есть следующее, что всегда отображает результаты не найдены, хотя там массив из API не пуст.

<div class="col-md-6">
  <label ng-i18next="candidateAdd.formerCompany"></label>
  <input 
    type="text" 
    class="form-control m-input" 
    ng-model="vm.candidate.former_company" 
    uib-typeahead="company for company in vm.searchCompanies($viewValue)" 
    typeahead-loading="loading" 
    typeahead-no-results="noResults" />
    <i ng-show="loading" class="glyphicon glyphicon-refresh"></i>
    <div ng-show="noResults">
      <i class="glyphicon glyphicon-remove"></i> No Results Found
    </div>
</div>

Метод контроллера:

searchCompanies(keyword) {
  const self = this;
  this.ClientsService
    .searchCompanies(keyword)
    .then(data => {
      return data;
    });
}

Способ обслуживания:

this.searchCompanies = function (keyword) {
  let deferred = $q.defer();
  let url = `${API_BASE_URL}/candidate/former_companies/?search=${keyword}`;

  $http.get(url)
    .then(res => {
      deferred.resolve(res.data);
    })
    .catch(res => {
      deferred.reject(res.data);
      $log.error(res.data);
    });

  return deferred.promise;
};

1 Ответ

0 голосов
/ 02 мая 2018

Вы должны вернуть все, что даст ClientsService, в противном случае вы достигнете конца функции и по умолчанию вернет undefined.

vm.searchCompanies(keyword) {
  const self = this;
  return this.ClientsService
    .searchCompanies(keyword)
    .then(data => {
      return data;
    });
}
...