mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
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 #2546
This commit is contained in:
parent
6452707d40
commit
1d8e11ddfb
2 changed files with 21 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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>' +
|
||||
|
|
|
|||
Loading…
Reference in a new issue