angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Support disabling options in select with ng:options

Open esprehn opened this issue 15 years ago • 96 comments

Need a way to construct

<select>
  <option disabled>A</option>
  <option>B</option>
  <optionC</option>
</select>

There's no way to express the disabled state of a specific option with ng:options

esprehn avatar Nov 05 '11 22:11 esprehn

+1

lucsky avatar Aug 21 '12 17:08 lucsky

+1

wilgert avatar Oct 26 '12 12:10 wilgert

I put together a directive as a hack around this limitation: http://jsfiddle.net/alalonde/dZDLg/5/

YMMV. What would a good syntax be to implement this in ng-options?

alalonde avatar Nov 08 '12 19:11 alalonde

+1 your directive is very good, but not sufficient:

model [{id: 1, title: 'Credit card'},{id:4, title: 'For free'}, {id: 7, title: 'Dont want it'}] select will looks like this:

when i want to disable i.e. "for free" option, you have to iterate through model and take array position for "For free" object (i.e 1). Problem is when angular ads empty option:

model[i] is shifted against expected data, so option[1] is "Credit card" not "For free"...

zalesak avatar Dec 16 '12 17:12 zalesak

Yes, I have added a number of enhancement to the directive in the meantime. Here is an updated version that should fix your problem: http://jsfiddle.net/alalonde/dZDLg/9/

alalonde avatar Dec 17 '12 19:12 alalonde

Thank you so much alalonde, you saved my day!

orlenko avatar Mar 04 '13 19:03 orlenko

+1

Coder2012 avatar Jun 28 '13 09:06 Coder2012

+1

estufa8 avatar Jul 10 '13 17:07 estufa8

It's very wierd. It works but it disables the previous option, so changing from locals[attr] = data[i]; to locals[attr] = data[i-1]; did the trick. I've tested it by placing a disabled: true property on the first and last item from the object to see if something breaks, but didn't break anything... it just works :)

chris-equis avatar Jul 29 '13 19:07 chris-equis

+1

terohe avatar Aug 23 '13 13:08 terohe

+1

exocom avatar Sep 25 '13 23:09 exocom

+1

acgentry avatar Sep 27 '13 14:09 acgentry

+1 Would be great having no dependency to jQuery

borgateo avatar Sep 30 '13 12:09 borgateo

as for data[i-1] - this is in general wrong, because 1 should be replaced number of options in HTML template.

maliarov avatar Oct 15 '13 14:10 maliarov

+1

chrsoo avatar Nov 10 '13 11:11 chrsoo

+1

linkswapnil avatar Nov 10 '13 13:11 linkswapnil

+1

bmulholland avatar Dec 18 '13 01:12 bmulholland

+1

durango avatar Dec 21 '13 05:12 durango

+1

vikasgulati avatar Dec 26 '13 12:12 vikasgulati

Custom directive without jQuery dependency.

http://stackoverflow.com/a/20790905/1283546

@borteo - Check this out!

vikasgulati avatar Dec 26 '13 20:12 vikasgulati