Skip to content

Commit d07172f

Browse files
author
Gonzalo Ruiz de Villa
committedMay 2, 2013
fix(ngRepeat): correctly iterate over array-like objects
Check if the object is array-like to iterate over it like it's done with arrays. Closes angular#2546
1 parent fc25a44 commit d07172f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
 

‎src/ng/directive/ngRepeat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
212212
nextBlockOrder = [];
213213

214214

215-
if (isArray(collection)) {
215+
if (isArrayLike(collection)) {
216216
collectionKeys = collection;
217217
} else {
218218
// if object, extract keys, sort them and use to determine order of iteration over obj props

‎test/ng/directive/ngRepeatSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ describe('ngRepeat', function() {
5555
});
5656

5757

58+
it('should iterate over an array-like object', function() {
59+
element = $compile(
60+
'<ul>' +
61+
'<li ng-repeat="item in items">{{item.name}};</li>' +
62+
'</ul>')(scope);
63+
64+
document.body.innerHTML = "<p>" +
65+
"<a name='x'>a</a>" +
66+
"<a name='y'>b</a>" +
67+
"<a name='x'>c</a>" +
68+
"</p>";
69+
70+
var htmlCollection = document.getElementsByTagName('a');
71+
scope.items = htmlCollection;
72+
scope.$digest();
73+
expect(element.find('li').length).toEqual(3);
74+
expect(element.text()).toEqual('x;y;x;');
75+
});
76+
77+
5878
it('should iterate over on object/map', function() {
5979
element = $compile(
6080
'<ul>' +

0 commit comments

Comments
 (0)
Please sign in to comment.