Setting options for typeahead, using Angular-ui Bootstrap

ktm picture ktm · Jul 18, 2013 · Viewed 13.5k times · Source

Using typeahead, I am trying to set a couple of options, which are available as standard Bootstrap typeahead options

Using the following code, I'm able to set the "minLength" property by putting it in-line as "typeahead-min-length", but doing the same with "items" as "typeahead-items" does not. Is there a different/better/standard way to do this? Is there a way to attach an array of options to $scope?

<div class='container-fluid' ng-controller="TestController">

<input type="text" ng-model="selected" 
typeahead="foo for foo in foo | filter:$viewValue" typeahead-items='5' typeahead-min-length='3'>

<pre>{{selected| json}}</pre>  </div>

Update: I'm trying to stick with just AngularJS, and not use jQuery

This is my controller:

  myAppModule.controller('TestController', function($http,$scope) {
  $scope.selected = undefined;

  $http.get('data/sample.json')
       .then(function(results){
          $scope.foo = results.data; 
      });
  });

Answer

Hieu Nguyen picture Hieu Nguyen · Jul 18, 2013

Why don't you use the JS way to initialize typeahead:

$('input.typeahead').typeahead({
    source: array_of_value_here,
    minLength: 1,
    items: 5,
    // your option values ...
});

EDIT:

I see, the AngularJS way, you can use limitTo:

<input type="text" ng-model="selected" typeahead="foo for foo in foo | filter:$viewValue | limitTo:5" typeahead-min-length='3'>