fix($rootScope) ensure $watchCollection correctly handles arrayLike objects

This commit is contained in:
Gonzalo Ruiz de Villa 2013-05-02 13:05:22 +02:00 committed by Pete Bacon Darwin
parent dc9a580617
commit 6452707d40
2 changed files with 18 additions and 1 deletions

View file

@ -376,7 +376,7 @@ function $RootScopeProvider(){
oldValue = newValue;
changeDetected++;
}
} else if (isArray(newValue)) {
} else if (isArrayLike(newValue)) {
if (oldValue !== internalArray) {
// we are transitioning from something which was not an array into array.
oldValue = internalArray;

View file

@ -463,6 +463,23 @@ describe('Scope', function() {
$rootScope.$digest();
expect(log).toEqual([ '[{},[]]' ]);
});
it('should watch array-like objects like arrays', function () {
var arrayLikelog = [];
$rootScope.$watchCollection('arrayLikeObject', function logger(obj) {
forEach(obj, function (element){
arrayLikelog.push(element.name);
})
});
document.body.innerHTML = "<p>" +
"<a name='x'>a</a>" +
"<a name='y'>b</a>" +
"</p>";
$rootScope.arrayLikeObject = document.getElementsByTagName('a')
$rootScope.$digest();
expect(arrayLikelog).toEqual(['x', 'y']);
});
});