1

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?

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting back the object from an API. I'll figure out if I can transform te object to an array in the API.

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.