File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -362,6 +362,7 @@ function $RootScopeProvider(){
362
362
$watchCollection : function ( obj , listener ) {
363
363
var self = this ;
364
364
var oldValue ;
365
+ var oldArray ;
365
366
var newValue ;
366
367
var changeDetected = 0 ;
367
368
var objGetter = $parse ( obj ) ;
@@ -371,6 +372,7 @@ function $RootScopeProvider(){
371
372
372
373
function $watchCollectionWatch ( ) {
373
374
newValue = objGetter ( self ) ;
375
+ oldArray = null ;
374
376
var newLength , key ;
375
377
376
378
if ( ! isObject ( newValue ) ) {
@@ -386,6 +388,8 @@ function $RootScopeProvider(){
386
388
changeDetected ++ ;
387
389
}
388
390
391
+ oldArray = oldValue . slice ( 0 ) ;
392
+
389
393
newLength = newValue . length ;
390
394
391
395
if ( oldLength !== newLength ) {
@@ -439,7 +443,7 @@ function $RootScopeProvider(){
439
443
}
440
444
441
445
function $watchCollectionAction ( ) {
442
- listener ( newValue , oldValue , self ) ;
446
+ listener ( newValue , oldArray || oldValue , self ) ;
443
447
}
444
448
445
449
return this . $watch ( $watchCollectionWatch , $watchCollectionAction ) ;
Original file line number Diff line number Diff line change @@ -510,6 +510,28 @@ describe('Scope', function() {
510
510
$rootScope . $digest ( ) ;
511
511
expect ( arrayLikelog ) . toEqual ( [ 'x' , 'y' ] ) ;
512
512
} ) ;
513
+
514
+ it ( 'should return a new array with old values' , function ( ) {
515
+ var watchArgs ;
516
+ $rootScope . $watchCollection ( 'obj' , function ( newValues , oldValues ) {
517
+ watchArgs = {
518
+ newValues : newValues ,
519
+ oldValues : oldValues
520
+ } ;
521
+ } ) ;
522
+
523
+ $rootScope . obj = [ 'a' ] ;
524
+ $rootScope . $digest ( ) ;
525
+
526
+ expect ( watchArgs . newValues ) . toEqual ( $rootScope . obj ) ;
527
+ expect ( watchArgs . oldValues ) . toEqual ( [ ] ) ;
528
+
529
+ $rootScope . obj . push ( 'b' ) ;
530
+ $rootScope . $digest ( ) ;
531
+
532
+ expect ( watchArgs . newValues ) . toEqual ( [ 'a' , 'b' ] ) ;
533
+ expect ( watchArgs . oldValues ) . toEqual ( [ 'a' ] ) ;
534
+ } )
513
535
} ) ;
514
536
515
537
You can’t perform that action at this time.
0 commit comments