r/angularjs • • Aug 12 '16

Stop ng-repeating options: use ng-options

http://www.codelord.net/2016/08/12/stop-ng-repeating-options-use-ng-options/
41 Upvotes

11 comments sorted by

4

u/rickdg Aug 12 '16

Still haven't been able to get this to work for options that are defined asynchronously, as in loaded from $http for example.

1

u/abyx Aug 14 '16

Can you provide an example? This (very very stupid) plunk works for me (using $timeout to simulate async code).

3

u/fernker Aug 12 '16

TIL you can have a disabled option in a select list.

5

u/akujinhikari Aug 12 '16

This is a HUGE pet peeve of mine, and I had to refactor about 10 of these when I got to a new project at work. Then I wrote a company-wide post about it on our intranet site.

8

u/guybrushthr33pwood Aug 12 '16

Which nobody ever read and they continue to do it wrong to this day ;)

0

u/akujinhikari Aug 12 '16

It had 25 views (out of ~1,000 devs worldwide) and 2 comments praising me. But, yeah... still done wrong. ha ha

3

u/enkideridu Aug 13 '16

Out of curiosity, what kind of stuff do you make with 1k devs?

1

u/akujinhikari Aug 13 '16

Websites. Seriously though we're a large company with many big-name clients.

4

u/zwacky Aug 13 '16

honestly I ran into more problems using ng-options than using ng-repeat with options:

  • problems with async option loadings and preselection

  • makes me looking up its syntax every time

  • in some cases you need to be in control of the options yourself

I don't see there being a wrong or right. Clearly it's meant to be a shortcut to write less and more consistent code. But the above mentioned reasons make me just use ng-repeat with options.

2

u/simkessy Aug 12 '16

I went back to my angular-app to see if I do this and realized I just use ui-select

2

u/herrherrmann Aug 13 '16

Yes, that one's definitely a good choice if you need some more power (searchable list of options, retrieving options asynchronously, custom templates with styling, icons etc.).