Jeffrey
Jeffrey

Reputation: 2005

Use uib-typeahead to loop through an object

I'm trying to use uib-typeahead to loop through an object but I can't figure out what to put in the uib-typeahead argument.

My HTML is:

<input type="text" ng-model="selected" uib-typeahead="option for option in options" class="form-control">

And my Angular code is:

$scope.options = {
    22: "Abc",
    27: "Def",
    55: "Ghi"
}

If I type anything right now I don't see anything. I would like to get the key value in the input when I selected one of the options. What should I put in the uib-typeahead argument?

Upvotes: 1

Views: 1019

Answers (1)

Robert Corey
Robert Corey

Reputation: 703

You're using an object to populate the dropdown instead of an array. Here's a plunker showing it working correctly with an array http://plnkr.co/edit/8VkpF4pKHBMKrPQPtnkT?p=preview

angular.module('plunker', ['ui.bootstrap']);
function TypeaheadCtrl($scope) {
  $scope.options = [
    "Abc",
    "Def",
    "Ghi"
  ];
}

You can transform the object into an array like this

var obj = {1: 'a', 2: 'b', 3: 'c'};

var arr1 = [];
for (var i in obj) {
  arr1.push(obj[i]);
}
// or you can use lodash https://lodash.com/
var arr2 = _.map(obj);
//both result in ['a', 'b', 'c']

Upvotes: 1

Related Questions