Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Typeahead doesn't support two-way data binding fully... #274

Closed
Siyfion opened this issue Mar 27, 2013 · 12 comments
Closed

Typeahead doesn't support two-way data binding fully... #274

Siyfion opened this issue Mar 27, 2013 · 12 comments

Comments

@Siyfion
Copy link

Siyfion commented Mar 27, 2013

See the plunkr here: http://plnkr.co/edit/3ryOYo

If the typeahead is to truly use "the same, flexible syntax as the select directive (http://docs.angularjs.org/api/ng.directive:select)" then it needs to support two-way binding via this method.

@pkozlowski-opensource
Copy link
Member

@Siyfion In reality is more complex than this. The thing is that the typeahead can work with results retrieved from a server and in the case you are binding id only there is simply not have enough info to render input's value.

In the future I'm going to add an option so people can specify a function that would be called on the initial render but for now you could plug into NgModelController#$formatters pipeline.

Anyway, thnx for opening this issue, this is something to be documented at least.

@Siyfion
Copy link
Author

Siyfion commented Mar 27, 2013

I don't suppose I could get you to elaborate on how the plugging into NgModelController#$formatters pipeline would work?

@jc-tzn
Copy link

jc-tzn commented Apr 20, 2013

Any news on this issue since its opening? I started reading about NgModelController but any extra indications would be very much appreciated ;)

@pkozlowski-opensource
Copy link
Member

@jc-tzn @Siyfion Sorry guys, I kind of missed this one, was working on the positioning issues for the last couple of days.

Coming back to this issue the main trouble is that given an array of objects retrieved from the server, lets say an array of states like this: [{code: 'AL', name: 'Alaska'}] people might choose to bind the code only to the model but still use a more complex expression to render a dropdown's items. This is exactly what is happening in this expression:

typeahead="state.code as state.name for state in states | filter:$viewValue"

Then, when the input content is initially rendered with this binding we've got only state.code but would like to render 'state.name'. But we don't have the state in the model, only its code!

The only solution for this issue I can think of right now is to allow people to specify a function that would be responsible for retrieving the whole object based on a value bound to the model (here retrieving the whole state object based on its code). But then I've realized that such a function is exactly a formatter from the NgModelController: http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController#$formatters Some more info about those in the "Custom Validation" section of this doc: http://docs.angularjs.org/guide/forms
But we've got an additional complexity here as a formatting function must be capable of returning a matching element from a back-end.

Hope that above makes sense and you guys will give it a go and open an awesome pull request. Or you will see that I'm talking rubbish and open an awesome pull request with an alternative solution :-) If not I will eventually go back to this after all the positioning issues are sorted out.

@jc-tzn
Copy link

jc-tzn commented Apr 21, 2013

thanks for the quick answer, I had a look at all that but it's kinda beyond my current understanding of angular unfortunately (my brain hurts now) so I'll try to find a simpler work-around to my problem (getting typeahead to work with socket.io) or I'll wait for you to do some of your magic :)

@jc-tzn
Copy link

jc-tzn commented Apr 21, 2013

Aaand I found one ! I read the issue about "typeahead on callback" (#252) so I tried using $q and defer and it worked :)

@Siyfion
Copy link
Author

Siyfion commented Jun 21, 2013

Hey @pkozlowski-opensource I'm taking a bit more of a look at this again now, so regarding your comment about using a formatter to retrieve the related data for the model, how does one go about updating the typeahead's value?

Using your example:
ng-model="userInfo.stateCode" typeahead="state.code as state.name for state in states | filter:$viewValue"

Say I load a model into the userInfo object and it has a stateCode of NY, how would I go about using a formatter to insert the value back into the typeahead? I know that I'm going to need to have a $http.get() request to retrieve the additional information from the server, but after that, I can't see how to force it back in to the typeahead...?

@ProLoser
Copy link
Member

Welcome to every problem I had to deal with while building select2 and ui-select2 (yes, I contributed to both). Perhaps we should make a new native alternative standalone for both of these @pkozlowski-opensource ?

@szaccaria
Copy link

Hello @pkozlowski-opensource I read that this issue is was closed... I've this jfiddle https://jsfiddle.net/qq4gqn6t/12/ but when I change the model the label not change... Can help me?
Thanks

@icfantv
Copy link
Contributor

icfantv commented Jan 27, 2016

@szaccaria, support-related questions are best suited by posting them to StackOverflow where there's a significantly wider audience to get them answered.

@szaccaria
Copy link

@icfantv done! thanks for suggest!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants