trap
trap

Reputation: 2640

autocomplete in angular with typehead

I tried to do an autocomplete in angular with typehead, but it is not working.

I downloaded the typehead.js from angularstrap page.

javascript:

myApp.controller('myCtrl', ['$scope', 'myservice', function ($scope, myservice) {
    $scope.counter = 0;
    $scope.name= [];
    myservice.getData().then(function (msg) {
        $scope.name= msg.data.names;
    });

    $scope.input = "";

}]);

html:

 <div ng-controller="myCtrl">
    <input type="text" class="form-control" ng-model="input" 
      bs-options="name for name in names" bs-typeahead>

 </div>

What have I done wrong?

If there is a better solution for autocomplete example pls tell me. thx in advance

Upvotes: 0

Views: 194

Answers (1)

Darren Reid
Darren Reid

Reputation: 2322

Your $scope.name should be $scope.names if you want it to work with the expression you are using in bs-options. Eg

Javascript:

myApp.controller('myCtrl', ['$scope', 'myservice', function ($scope, myservice) {
    $scope.counter = 0;
    $scope.names= [];
    myservice.getData().then(function (msg) {
        $scope.names= msg.data.names;
    });

    $scope.input = "";

}]);

I've created an example plunker to show your code working with the fix above.

Upvotes: 4

Related Questions