@@ -2316,6 +2316,40 @@ describe('select', function() {
2316
2316
2317
2317
} ) ;
2318
2318
2319
+ it ( 'should keep the ngModel value when the selected option is recreated by ngRepeat' , function ( ) {
2320
+ scope . options = [ { name : 'A' } , { name : 'B' } , { name : 'C' } ] ;
2321
+ scope . obj = {
2322
+ value : 'B'
2323
+ } ;
2324
+
2325
+ compile (
2326
+ '<select ng-model="obj.value">' +
2327
+ '<option ng-repeat="option in options" value="{{option.name}}">{{option.name}}</option>' +
2328
+ '</select>'
2329
+ ) ;
2330
+
2331
+ var optionElements = element . find ( 'option' ) ;
2332
+ expect ( optionElements . length ) . toEqual ( 3 ) ;
2333
+ expect ( optionElements [ 0 ] . value ) . toBe ( 'A' ) ;
2334
+ expect ( optionElements [ 1 ] ) . toBeMarkedAsSelected ( ) ;
2335
+ expect ( scope . obj . value ) . toBe ( 'B' ) ;
2336
+
2337
+ scope . $apply ( function ( ) {
2338
+ // Only when new objects are used, ngRepeat re-creates the element from scratch
2339
+ scope . options = [ { name : 'B' } , { name : 'C' } , { name : 'D' } ] ;
2340
+ } ) ;
2341
+
2342
+ var previouslySelectedOptionElement = optionElements [ 1 ] ;
2343
+ optionElements = element . find ( 'option' ) ;
2344
+
2345
+ expect ( optionElements . length ) . toEqual ( 3 ) ;
2346
+ expect ( optionElements [ 0 ] . value ) . toBe ( 'B' ) ;
2347
+ expect ( optionElements [ 0 ] ) . toBeMarkedAsSelected ( ) ;
2348
+ expect ( scope . obj . value ) . toBe ( 'B' ) ;
2349
+ // Ensure the assumption that the element is re-created is true
2350
+ expect ( previouslySelectedOptionElement ) . not . toBe ( optionElements [ 0 ] ) ;
2351
+ } ) ;
2352
+
2319
2353
} ) ;
2320
2354
2321
2355
0 commit comments