Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 6452707

Browse files
Gonzalo Ruiz de Villapetebacondarwin
Gonzalo Ruiz de Villa
authored andcommitted
fix($rootScope) ensure $watchCollection correctly handles arrayLike objects
1 parent dc9a580 commit 6452707

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/ng/rootScope.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ function $RootScopeProvider(){
376376
oldValue = newValue;
377377
changeDetected++;
378378
}
379-
} else if (isArray(newValue)) {
379+
} else if (isArrayLike(newValue)) {
380380
if (oldValue !== internalArray) {
381381
// we are transitioning from something which was not an array into array.
382382
oldValue = internalArray;

test/ng/rootScopeSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,23 @@ describe('Scope', function() {
463463
$rootScope.$digest();
464464
expect(log).toEqual([ '[{},[]]' ]);
465465
});
466+
467+
it('should watch array-like objects like arrays', function () {
468+
var arrayLikelog = [];
469+
$rootScope.$watchCollection('arrayLikeObject', function logger(obj) {
470+
forEach(obj, function (element){
471+
arrayLikelog.push(element.name);
472+
})
473+
});
474+
document.body.innerHTML = "<p>" +
475+
"<a name='x'>a</a>" +
476+
"<a name='y'>b</a>" +
477+
"</p>";
478+
479+
$rootScope.arrayLikeObject = document.getElementsByTagName('a')
480+
$rootScope.$digest();
481+
expect(arrayLikelog).toEqual(['x', 'y']);
482+
});
466483
});
467484

468485

0 commit comments

Comments
 (0)