使用uib-typeahead循环遍历对象

时间:2016-02-18 15:22:36

标签: angularjs angular-ui-typeahead

我正在尝试使用uib-typeahead来遍历一个对象,但我无法弄清楚要放在uib-typeahead参数中的内容。

我的HTML是:

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

我的Angular代码是:

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

如果我现在输入任何东西,我什么也看不见。当我选择其中一个选项时,我想在输入中获取键值。我应该在uib-typeahead论证中加入什么?

1 个答案:

答案 0 :(得分:1)

您正在使用对象填充下拉列表而不是数组。这是一个使用数组http://plnkr.co/edit/8VkpF4pKHBMKrPQPtnkT?p=preview

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

您可以将对象转换为像这样的数组

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']