对于返回字符串数组的异步api,uib-typeahead的正确值是什么?

时间:2018-04-27 16:41:47

标签: angularjs angular-ui-bootstrap

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 :(得分:1)

您应该返回ClientsService给出的任何内容,否则您将到达该函数的末尾,默认情况下它将返回undefined

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