Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Bootstrap typeahead with Angular

Tags:

I am currently developing a web application which uses twitter-bootstrap and Angularjs in good harmony. However, I have problems with the typeahead and using it as a ng-model.

Everything works fine when typing, but when I select an item (a suggestion), the value does not reflect in the Angular controller unless I change the value of the textbox after a value has been selected. Type -> Select -> Type works. Type -> Select does not work.

HTML:

<form ng-submit="addAssignment(assignName)">
  <div class="input-append">
    <input type="text" placeholder="Type a name" ng-model="assignName" ng-change="dostuff()" data-provide="typeahead" data-source="{{ teamNames }}">
    <button class="btn" type="submit">Add</button>
 </div>
</form>

Angular code:

 $scope.addAssignment = function(name) {
    alert(name);
    return;
 }

I have added a ng-change function just to check when the model value is changed. It is only changed when typing manually, and NOT when a value is selected from the list that appears on typeahead.

I appreciate any response that may help to resolve this issue. Thanks!

like image 797
Kjetil Haaheim Avatar asked Oct 01 '12 20:10

Kjetil Haaheim


2 Answers

I would suggest checking out the typeahead directive from the AngularUI/boostrap repository: http://angular-ui.github.com/bootstrap/

It is native implementation in pure AngularJS so it doesn't require any 3rd party dependencies. On top of this it is very well integrated with the AngularJS ecosystem as it: * uses concise syntax known from the select directive * understands AngularJS promises so results can be fetched dynamically using $http with the minimal effort.

like image 194
pkozlowski.opensource Avatar answered Oct 09 '22 11:10

pkozlowski.opensource


There is a working native implementation in AngularStrap for Bootstrap3 that leverages ngAnimate from AngularJS v1.2+

  • Demo : http://mgcrea.github.io/angular-strap/##typeaheads

You may also want to checkout:

  • Source : https://github.com/mgcrea/angular-strap/blob/master/src/typeahead/typeahead.js
  • Plunkr : http://plnkr.co/edit/VB43wxoDBhVyRMnKUHr9?p=preview
like image 22
Olivier Avatar answered Oct 09 '22 10:10

Olivier