2

Below is my HTML code for 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>

Below is JS File:

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();
}]);

The problem is I am trying to fetch the branch name in the input field and as I type the below table data should get filtered according to that. The onChange function is working for select box. The JSON format of the api url is as follows:

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

Any Idea how to modify this? Getting stuck. Please help me.

3
  • @zmbq you edited my code do you know what's wrong with my code? Commented Sep 24, 2016 at 18:32
  • Are you using typeahead from AngularJS UI Bootstrap here? If so you need to change typeahead to uib-typeahead in your search input tag Commented Sep 24, 2016 at 18:53
  • @zmbq Yeah I am using this one Commented Sep 24, 2016 at 19:00

1 Answer 1

2

You have mistaken while using typeahead directive, the way it works is, it will need a promise object on which you will apply $viewValue filter

<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=""/>

So lets change your $scope.getData function to return promise object from it.

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

Note: If you are using Angular ui-bootstrap version 1.X+ then you have to use uib- prefix before each ui-bootstrap directive like in your case it would be uib-typeahead instead of typeahead.

Sign up to request clarification or add additional context in comments.

6 Comments

@PreetySingh you have to use uib-typeahead then. :)
error in console :( Argument 'ServicesCtrl' is not a function, got string
Are you sure you have referred JS file on html page?
Check if you have mistakenly remove some your JS reference, open browser console "Sources" tab & check does your desired file has been loaded or not?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.