Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use AngularJS + UI Bootstrap Typeahead to fill in input field with name attribute of an object while I need to use id attribute of this object when the form is sent.

The problem is that when I try to preload the input with some previously saved value - the value of id attribute is displayed inside the input instead of the value of name attribute.

<input type="text" ng-model="form.product_id" typeahead="option.id as option.name for option in p.options | filter:$viewValue" />

Fiddle is here: http://jsfiddle.net/honzzz/T3YFR/3/

How do I preload the value of name attribute and still keep the desired functionality?

share|improve this question
ur fiddle not working though – iBlue Jul 16 at 10:21
I see that it's not working in new builds of Chrome - it's not loading external scripts from github because strict MIME type checking is enabled. If you want to fiddle with it could you please use Firefox? – honzzz Jul 16 at 11:40
updated code have a look at my answer – iBlue Jul 16 at 11:49
1  
Same fiddle but working: jsfiddle.net/hieuh25/CXnKD – Hieu Nguyen Jul 19 at 9:26
add comment (requires an account with 50 reputation)

This question has an open bounty worth +50 reputation from honzzz ending in 2 days.

This question has not received enough attention.

1 Answer

up vote 2 down vote accepted

You can use the product object as ng-model. And then you can simply get the id use form.product.id.

Demo on jsFiddle

<div ng-controller="MyTabCtrl"> <pre>Model: {{form.product.id | json}}</pre>
    <p>The goal is to show "Horay!" while the model is set to 1.</p>
        <input type="text" autocomplete="off" ng-model="form.product" typeahead="option as option.name for option in p.options | filter:$viewValue" />
    <p>Try to write "Audi".</p>
</div>
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.