我一直在这里使用 type advance 指令,效果很好: http://angular-ui.github.io/bootstrap/#/typeahead
我的问题是,我已经摆脱了使用整个对象的场景:
<input type="text" ng-model="MyPerson" typeahead="i as i.Name for i in PeopleSearch($viewValue)" />
对于我只想要对象的一部分的场景:
<input type="text" ng-model="MyPerson" typeahead="i.Id as i.Name for i in PeopleSearch($viewValue)" />
此时,无论为“as”输入什么,模型和文本框都将包含相同的内容。谁能解释为什么会发生这种情况?看起来逻辑应该模仿 ng-options 的表达式,但显然事实并非如此。
这是一个演示:http://plnkr.co/edit/5oqlldC3PltnrynqhnAU?p=preview
选择项目后,文本框应包含此人的姓名。
最佳答案
这是我的解决方案,以防其他人遇到这种情况:
<input type="text" ng-model="MySelectedPerson" typeahead="i as i.Name for i in PeopleSearch($viewValue)" typeahead-on-select="MyPerson = MySelectedPerson.Id" />
关于angularjs - Angular Bootstrap Type Ahead 与 typeahead 语法相关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21788397/