繁体   English   中英

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

[英]Angular-UI typeahead results not showing in dropdown

我使用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); 
    });     
}

在我的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 -->

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

你的函数getLocationForSearch()不好:它必须返回一个对uib-typeahead指令的promise 所以工作代码是:

  $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);
    });
  }

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM