@@ -116,10 +116,10 @@ var SelectController =
116
116
} ;
117
117
118
118
119
- self . registerOption = function ( optionScope , optionElement , optionAttrs , interpolateValueFn , interpolateTextFn ) {
119
+ self . registerOption = function ( optionScope , optionElement , optionAttrs , hasDynamicValueAttr , interpolateTextFn ) {
120
120
121
- if ( interpolateValueFn ) {
122
- // The value attribute is interpolated
121
+ if ( hasDynamicValueAttr ) {
122
+ // either " value" is interpolated directly, or set by ngValue
123
123
var oldVal ;
124
124
optionAttrs . $observe ( 'value' , function valueAttributeObserveAction ( newVal ) {
125
125
if ( isDefined ( oldVal ) ) {
@@ -165,7 +165,9 @@ var SelectController =
165
165
* When an item in the `<select>` menu is selected, the value of the selected option will be bound
166
166
* to the model identified by the `ngModel` directive. With static or repeated options, this is
167
167
* the content of the `value` attribute or the textContent of the `<option>`, if the value attribute is missing.
168
- * If you want dynamic value attributes, you can use interpolation inside the value attribute.
168
+ * For dynamic options, use interpolation inside the `value` attribute or the `textContent`. Using
169
+ * {@link ngValue ngValue} is also possible (as it sets the `value` attribute), and will take
170
+ * precedence over `value` and `textContent`.
169
171
*
170
172
* <div class="alert alert-warning">
171
173
* Note that the value of a `select` directive used without `ngOptions` is always a string.
@@ -456,13 +458,19 @@ var optionDirective = ['$interpolate', function($interpolate) {
456
458
restrict : 'E' ,
457
459
priority : 100 ,
458
460
compile : function ( element , attr ) {
459
- if ( isDefined ( attr . value ) ) {
460
- // If the value attribute is defined, check if it contains an interpolation
461
- var interpolateValueFn = $interpolate ( attr . value , true ) ;
461
+ var hasDynamicValueAttr , interpolateTextFn ;
462
+
463
+ if ( isDefined ( attr . ngValue ) ) {
464
+ // If ngValue is defined, then the value attr will be set to the result of the expression,
465
+ // and the selectCtrl must set up an observer
466
+ hasDynamicValueAttr = true ;
467
+ } else if ( isDefined ( attr . value ) ) {
468
+ // If the value attr contains an interpolation, the selectCtrl must set up an observer
469
+ hasDynamicValueAttr = $interpolate ( attr . value , true ) ;
462
470
} else {
463
471
// If the value attribute is not defined then we fall back to the
464
472
// text content of the option element, which may be interpolated
465
- var interpolateTextFn = $interpolate ( element . text ( ) , true ) ;
473
+ interpolateTextFn = $interpolate ( element . text ( ) , true ) ;
466
474
if ( ! interpolateTextFn ) {
467
475
attr . $set ( 'value' , element . text ( ) ) ;
468
476
}
@@ -477,7 +485,7 @@ var optionDirective = ['$interpolate', function($interpolate) {
477
485
parent . parent ( ) . data ( selectCtrlName ) ; // in case we are in optgroup
478
486
479
487
if ( selectCtrl ) {
480
- selectCtrl . registerOption ( scope , element , attr , interpolateValueFn , interpolateTextFn ) ;
488
+ selectCtrl . registerOption ( scope , element , attr , hasDynamicValueAttr , interpolateTextFn ) ;
481
489
}
482
490
} ;
483
491
}
0 commit comments