Typeahead using object name

2019-02-04 17:07发布

I'm trying to setup a typeahead using AngularJS & UI Bootstrap like so:

.html

<input type="text" ng-model="selectedStuff" typeahead="stuff.name for stuff in stuffs | filter:$viewValue"/>

<span>{{selectedStuff.name}}</span>
<span>{{selectedStuff.desc}}</span>

.js

$scope.stuffs= [
                {
                 "name":"thing1",
                 "desc":"this is the first thing"
                },
                {
                 "name":"thing2",
                 "desc":"this is the second thing"
                }
               ]

Currently, I've been able to update the model with the selected name, but my goal is to pass along the entire object via the typeahead. Is there a clean way to do this using only the input?

2条回答
老娘就宠你
2楼-- · 2019-02-04 17:56

Sure thing :-)

The typeahead directive from http://angular-ui.github.io/bootstrap/ uses the same super-flexible syntax as the AngularJS select directive for ng-options. So you could write:

typeahead="stuff as stuff.name for stuff in stuffs | filter:$viewValue"

Here is a working plunk: http://plnkr.co/edit/5kGZkNPZ7rIFfb4Rvxej?p=preview

查看更多
三岁会撩人
3楼-- · 2019-02-04 18:00

I had the same problem. I have solved it by

typeahead="country.name for country in countryList | filter:$viewValue | limitTo:8"

here countryList is a list of country object. name is one of property of country object. For me it is working fine.

查看更多
登录 后发表回答