【问题标题】:Angularjs Typeahead not showing dropdownAngularjs Typeahead 不显示下拉菜单
【发布时间】:2017-03-30 09:13:43
【问题描述】:

我正在尝试使用异步预输入,我可以很好地获取数据,并且检查器清楚地显示正在返回的数据。例如我正在使用getLocation的引导示例

$scope.getLocation = function(val) {
    return $http.get('//maps.googleapis.com/maps/api/geocode/json', {
      params: {
        address: val,
        sensor: false
      }
    }).then(function(response){
      return response.data.results.map(function(item){
        return item.formatted_address;
      });
    });
  };

这正是我希望它工作的方式,然后这就是我的方法。

$scope.filterClients =function(val) {
        var data = $.param({
            action: "filter_clients",
            value: val
        });
        var config = {headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'}}

        $http.post('http://***************.co.uk/switchboard.php', data, config)
            .then(function (response) {
                return response.data.map(function(item){
                    return item.first_name;
                  });
            });  
    };

HTML 代码

<input ng-model="asyncSelected" placeholder="Select a Client" uib-typeahead="first_name for first_name in filterClients($viewValue)" class="form-control"/>

【问题讨论】:

  • 显示html代码
  • @sachilaranawaka html 提供
  • 在http post请求前面也加一个return

标签: javascript php angularjs json


【解决方案1】:

你不会从$scope.filterClients 返回任何东西。只需像这样更改您的功能:

$scope.filterClients = function (val) {
    var data = $.param({
        action: "filter_clients",
        value: val
    });
    var config = {headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'}}

    return $http.post('http://***************.co.uk/switchboard.php', data, config)
            .then(function (response) {
                return response.data.map(function (item) {
                    return item.first_name;
                });
            });
};

注意

最好在返回数据之前确保一切正常,否则可能会出错。尝试这样的方法来避免错误:

return $http.post('http://***************.co.uk/switchboard.php', data, config).then(function (response) {
    if (!angular.isObject(response) || !angular.isDefined(response.data)) {
        /* this you can put to avoid any errors */
        return;
    }

    /* if you want to limit you can use this code */
    var limit = 10;
    var data = [];
    for (var i = 0; i < response.data.length; i++) {
        if (i == limit) {
            break;
        }
        data.push(response.data[i]);
    }

    return data;
});

【讨论】:

  • 工作得很好,有时它是最明显的问题,当它允许我时会标记为答案,再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2022-01-01
相关资源
最近更新 更多