【问题标题】:Typeahead fetch data from url json value not workingTypeahead 从 url json 值中获取数据不起作用
【发布时间】:2017-02-02 10:14:04
【问题描述】:

下面是我的 angular typeahead 的 HTML 代码:

<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"
}]

任何想法如何修改这个?卡住。请帮帮我。

【问题讨论】:

  • @zmbq 你编辑了我的代码你知道我的代码有什么问题吗?
  • 您是否使用来自 AngularJS UI Bootstrap here 的预输入?如果是这样,您需要在搜索输入标签中将 typeahead 更改为 uib-typeahead
  • @zmbq 是的,我正在使用这个

标签: javascript angularjs twitter-bootstrap angular-ui-bootstrap bootstrap-typeahead


【解决方案1】:

你在使用 typeahead 指令时弄错了,它的工作方式是,它需要一个 promise 对象,你将在其上应用 $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-bootstrap 指令之前使用 uib- 前缀,就像您的情况一样,它应该是 uib-typeahead typeahead.

【讨论】:

  • 我正在使用这个
  • @PreetySingh 你必须使用uib-typeahead 然后。 :)
  • 控制台错误:(参数'ServicesCtrl'不是函数,得到字符串
  • 你确定你在html页面上引用了JS文件吗?
  • 检查您是否错误地删除了一些 JS 引用,打开浏览器控制台“Sources”选项卡并检查您想要的文件是否已加载?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2015-02-12
相关资源
最近更新 更多