Angular-UI预先输出结果未显示在下拉列表中

poo*_*666 11 javascript angularjs angular-ui-bootstrap angular-ui-typeahead

我正在使用Angular-ui typeahead组件来命中自动完成API,而我正在解析我回到一个名为的数组中的数据resp.但是,我没有看到数据传递到UI中的自动完成下拉列表.顺便说一句,控制器有一个console.log,可以正确显示数据,所以我知道它从api调用返回.

这是我的控制器功能:

$scope.getLocationForSearch = function(locationString){

    $scope.locationString = locationString;
    var url = '/autoComplete/' + locationString ;
    $http({
        method: 'GET',
        url: url            
    }).then(function successCallback(response) {
        console.clear();
        var resp = response.data.RESULTS.map(function(item){
            console.log(item.name);
            return item.name;
        });

        return resp;

      }, function errorCallback(response) {
        console.log(response); 
    });     
}
Run Code Online (Sandbox Code Playgroud)

在我的HTML中:

    <div class="input-group">
        <input 
            type="text" class="form-control" ng-model="asyncSelected" placeholder="Search city or zip code"
            uib-typeahead="item for item in getLocationForSearch($viewValue)"/>
        <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
        <div ng-show="noResults">
          <i class="glyphicon glyphicon-remove"></i> No Results Found
        </div>
        <span class="input-group-btn">
            <button class="btn btn-default" name="search" ng-model="asyncSelected" type="submit" ng-click="getWeather(asyncSelected)">
                <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
            </button>
        </span>
    </div><!-- /input-group -->
Run Code Online (Sandbox Code Playgroud)

这里有几个帖子用于同一个问题,但没有一个真正回答我的具体问题.任何帮助表示赞赏.

bea*_*ver 13

你的功能getLocationForSearch()是不好的:它必须返回一个承诺uib-typeahead指令.所以工作代码是:

  $scope.getLocationForSearch = function(locationString) {

    $scope.locationString = locationString;
    var url = '/autoComplete/' + locationString ;

    return $http({
      method: 'GET',
      url: url
    }).then(function successCallback(response) {
      console.clear();
      return response.data.results.map(function(item) {
        console.log(item.name);
        return item.name;
      });
    }, function errorCallback(response) {
      console.log(response);
    });
  }
Run Code Online (Sandbox Code Playgroud)

以下是Plunker的一个工作示例:

http://plnkr.co/edit/v67vR8f3nHImGSoAUyBd?p=preview

  • 你必须返回$ http()函数的输出,这是一个promise,请参阅https://docs.angularjs.org/api/ng/service/$http.相反,您尝试返回resp,它是已解析的promise中的响应.这是另一回事. (3认同)