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

Commit

Permalink
fix(ngRepeat): correctly iterate over array-like objects
Browse files Browse the repository at this point in the history
Check if the object is array-like to iterate over it like it's done with arrays.

Closes #2546
  • Loading branch information
Gonzalo Ruiz de Villa authored and petebacondarwin committed May 2, 2013
1 parent 6452707 commit 1d8e11d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
nextBlockOrder = [];


if (isArray(collection)) {
if (isArrayLike(collection)) {
collectionKeys = collection;
} else {
// if object, extract keys, sort them and use to determine order of iteration over obj props
Expand Down
20 changes: 20 additions & 0 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ describe('ngRepeat', function() {
});


it('should iterate over an array-like object', function() {
element = $compile(
'<ul>' +
'<li ng-repeat="item in items">{{item.name}};</li>' +
'</ul>')(scope);

document.body.innerHTML = "<p>" +
"<a name='x'>a</a>" +
"<a name='y'>b</a>" +
"<a name='x'>c</a>" +
"</p>";

var htmlCollection = document.getElementsByTagName('a')
scope.items = htmlCollection;
scope.$digest();
expect(element.find('li').length).toEqual(3);
expect(element.text()).toEqual('x;y;x;');
});


it('should iterate over on object/map', function() {
element = $compile(
'<ul>' +
Expand Down

0 comments on commit 1d8e11d

Please sign in to comment.