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

Commit

Permalink
fix(isArrayLike): Correct logic in isArrayLike to screen out objects
Browse files Browse the repository at this point in the history
- Fix ngRepeat check via isArrayLike check to prevent objects from
  accidentally passing as array-like
  • Loading branch information
Wesley Cho committed Nov 22, 2014
1 parent bb4d3b7 commit b9b5115
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function isArrayLike(obj) {
return true;
}

return isString(obj) || isArray(obj) || length === 0 ||
return isString(obj) || isArray(obj) || ((length === 0) && !isObject(obj)) ||
typeof length === 'number' && length > 0 && (length - 1) in obj;
}

Expand Down
2 changes: 0 additions & 2 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
}
collectionKeys.sort();
}

collectionLength = collectionKeys.length;
nextBlockOrder = new Array(collectionLength);

Expand Down Expand Up @@ -438,4 +437,3 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
}
};
}];

10 changes: 10 additions & 0 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,16 @@ describe('ngRepeat', function() {
expect(element.text()).toEqual('frodo:f:0|misko:m:1|shyam:s:2|');
});

it('should expose iterator offset as $index when iterating over objects with length key value 0', function() {
element = $compile(
'<ul>' +
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$index}}|</li>' +
'</ul>')(scope);
scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f', 'length':0};
scope.$digest();
expect(element.text()).toEqual('frodo:f:0|length:0:1|misko:m:2|shyam:s:3|');
});


it('should expose iterator position as $first, $middle and $last when iterating over arrays',
function() {
Expand Down

0 comments on commit b9b5115

Please sign in to comment.