简体   繁体   English

AngularJS预输入名称未定义

[英]Angularjs typeahead name undefined

I am getting undefined when the result is returned, can anyone help me out here? 返回结果时我变得不确定,有人可以在这里帮助我吗? I know there's a similar question out there but i couldn't understand the solution. 我知道那里也有类似的问题,但我不明白解决方案。 Thus, asking again. 因此,再次询问。

JS: JS:

 $scope.cnames = CombinedName;
 console.log($scope.cnames)

Console log view : 控制台日志视图:

在此处输入图片说明

Lastly, my html code : 最后,我的html代码:

      <input type="text" ng-model="selected" uib-typeahead="NAME 
     for cname in cnames | filter:$viewValue | limitTo:8">

View im getting : 查看即时消息:

在此处输入图片说明

You can just just bind it as , 您只需将其绑定为,

<input type="text" ng-model="selected" typeahead="cname for cname in cnames | filter:$viewValue | limitTo:8" class="form-control">

DEMO DEMO

Because the cnames array you are working with is a string, you have to use a specific form when iterating through the array. 因为要使用的cnames数组是一个字符串,所以在遍历该数组时必须使用特定的形式。 In general, ng-options / uib-typeahead expect the array you are iterating to be an array of objects. 通常, ng-options / uib-typeahead希望您要迭代的数组是对象数组。

When working with an array of strings, you can use 使用字符串数组时,可以使用

cname for cname in cnames

to correctly assign the text of the dropdown to the text of the array element. 将下拉菜单的文本正确分配给数组元素的文本。 This will set the selected value to the index of the selected element from the source array. 这会将selected值设置为源数组中所选元素的索引。

cname as cname for cname in cnames

would set the selected value as the text itself. 会将selected值设置为文本本身。

all the possible forms are documented at https://docs.angularjs.org/api/ng/directive/ngOptions . 所有可能的形式都在https://docs.angularjs.org/api/ng/directive/ngOptions中记录。 These are the same forms that are used by UI-Bootstrap. 这些是UI-Bootstrap使用的相同形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM