indrimuska / angular-selector

A native AngularJS directive that transform a simple <select> box into a full html select with typeahead.
http://indrimuska.github.io/angular-selector
MIT License
96 stars 36 forks source link

The initialization of default value should work even if the value is initialized before fetching the list #50

Closed kanikagupta13 closed 8 years ago

kanikagupta13 commented 8 years ago

The values in my selector are not getting initialized. The scenario is that I have a saved object in the database that I am fetching but my lists are getting populated after I fetch the item.

I think the watch on options should make share that the correct selected values are displayed. Below is the plunk for the behaviour.

http://plnkr.co/edit/D2wLxtgF2oOjTUVvdYrX?p=preview

In this my model browser1 is initalized prior to the list browsers1 is populated. As a result, the selected value does not get reflected in the list.

If I initialize the value after the population of the list, then it works fine. Below is the plunk depicting the correct behaviour.

http://plnkr.co/edit/UZIoYByVqSheTW2L8Eiq?p=preview

For now I have done a workaround in my code so that this works correctly. But is there any other way of doing this?

indrimuska commented 8 years ago

Hi @kanikagupta13, this should be handled using remote and remote-validation properties. Give me 5 minutes to write a plunker, in the meantime you can read about these properties in the README file. :)

indrimuska commented 8 years ago

Check out this plunker and let me know if it fits your needs :) http://plnkr.co/edit/PcbDYNZqdNxKoEnFWv63?p=preview

kanikagupta13 commented 8 years ago

hi @indrimuska . Thank you for the solution. :)

I was thinking if it is possible to use the remote or some other way without promises as well, then it will be great as I will have to inject promises everywhere in my app even for lists that are being loading synchronously. But for now, I will use this. Please let me know if there is any future scope of handling this without the use of promises.

Thank you for the quick solution. :+1: :)

indrimuska commented 8 years ago

Well, I don't think that it makes sense to have a remote property without a Promise. My advice for you is to understand the power of promises and the design pattern around them. You can also change your options without using a promise, but your code won't surely become so easy to handle and manage.

If you want to try, you can start using this plunker that doesn't make use of remote property: http://plnkr.co/edit/uyNiH8ozCW20PQ9tYoZT?p=preview

Good luck! :)