从url json中获取数据不工作

Typeahead fetch data from url json value not working

本文关键字:数据 工作 获取 url json      更新时间:2023-09-26

下面是我的HTML代码angular typeahead:

<div class="form-group">
 <select name="" class="form-control" ng-model="city" ng-change="onChange(e)" id="city">
    <option value="DELHI">DELHI</option>
    <option value="BANGALORE">BANGALORE</option>
    <option value="GOA">GOA</option>
    <option value="KOLKATA">KOLKATA</option>
    <option value="PUNJAB">PUNJAB</option>
 </select>
</div>
<input type="search" name="" ng-model="bankName" typeahead='bank_name as bankName | filter:$viewValue | limitTo:8'  id="input" class="form-control" value="" placeholder="Search" required="required" title="">
<table class="table table-hover">
    <thead>
        <tr>
            <th>IFSC</th>
            <th>BANK ID</th>
            <th>BANK NAME</th>
            <th>BRANCH</th>
            <th>ADDRESS</th>
            <th>CITY</th>
            <th>DISTRICT</th>
            <th>STATE</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="service in services">
            <td>{{service.ifsc}}</td>
            <td>{{service.bank_id}}</td>
            <td>{{service.bank_name}}</td>
            <td>{{service.branch}}</td>
            <td>{{service.address}}</td>
            <td>{{service.city}}</td>
            <td>{{service.district}}</td>
            <td>{{service.state}}</td>
        </tr>
    </tbody>
</table>
下面是JS文件:
var app = angular.module('bankBranch', ['ui.bootstrap'])

.controller('ServicesCtrl', ['$scope', '$http', function($scope, $http){
    $scope.bankUrl = "https://someweb.com/api/bank_branches?offset=0&limit=50&city=";
    $scope.city = 'BANGALORE';
    var bankName = '$scope.bank_name';  
    var cities = '$scope.city';
    $scope.getdata = function(){
        $http.get($scope.bankUrl+$scope.city).then(function(response){
            $scope.services = response.data;
        });
    }
    $scope.onChange = function(e){
        $scope.getdata();
    }
    $scope.getdata();
}]);

问题是我试图在输入字段中获取分支名称,当我键入下面的表数据时,应该根据该数据进行过滤。onChange函数正在为选择框工作。api url的JSON格式如下:

[{
"ifsc": "asdads",
"bank_id": 110,
"bank_name": "abc bank",
"branch": "BANGALORE",
"address": "ewrwerewr",
"city": "BANGALORE",
"district": "BANGALORE URBAN",
"state": "KARNATAKA"
}]

知道如何修改这个吗?越来越困。

您在使用typeahead指令时犯了错误,它的工作方式是,它需要一个承诺对象,您将在其上应用$viewValue过滤器

<input type="search" name="" ng-model="bankName" 
   typeahead='bank_name for bankName in getdata()'
   id="input" class="form-control" value="" 
   placeholder="Search" required="required" title=""/>

让我们改变你的$scope.getData函数从它返回承诺对象。

$scope.getdata = function(){
    return $http.get($scope.bankUrl+$scope.city).then(function(response){
        $scope.services = response.data;
        return $scope.services;
    });
}
$scope.onChange = function(e){
    return $scope.getdata();
}

注意:如果你使用的是Angular ui-bootstrap版本1。X+然后你必须在每个ui引导指令之前使用uib-前缀,就像在你的情况下,它将是uib-typeahead而不是typeahead