typeahead函数不会从我的JSON返回值

时间:2016-09-24 23:54:10

标签: angularjs json http

该脚本不仅会从我的JSON返回我需要的值。我需要来自JSON的名字
当我在输入字段中输入某个值时,以下内容会生成自动建议。 参考:http://plnkr.co/edit/08vi4ncjLrWfUBjWrZKS?p=preview

但是,这不会在我的代码中返回任何JSON值:

vector<string> vText(200) // You'd have space for 200 strings of chars ("h" for example)

1 个答案:

答案 0 :(得分:1)

您正在使用response而不是res

       <script>
        var app = angular.module('myApp', ['ui.bootstrap']);
        app.controller('myController', function($scope, $http){
            $scope.getAirport = function(inp){
                return $http.get('https://raw.githubusercontent.com/vedvasa/airports/master/airports.json', {
                  params: {
                    name : inp,
                    sensor : false
                  }

                }).then(function(response){ // <---- I changed this to response
                    var names = [];
                    angular.forEach(response.data.records, function(item){
                      names.push(item.addedNames);
                    });
                    return names;
                  });
            };

                  $scope.on_item_selected=function($item, $model, $label)
                  {
                      $scope.selected_item = $item;
                  }
        });
        </script>



        <div ng-app="myApp" ng-controller="myController">

            <form>
                <input type="text" class="form-control" id="source" placeholder="Enter Airport Code or City Name" ng-model="asyncSelected" typeahead="name for name in getAirports($viewValue)" typeahead-loading="loadingAirports" typeahead-on-select="on_item_selected($item, $model, $label)">
            </form>