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

fix(isArrayLike) Correctly detect arrayLike items #3356

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ var /** holds major version number for IE or NaN for real browsers */
* @return {boolean} Returns true if `obj` is an array or array-like object (NodeList, Arguments, ...)
*/
function isArrayLike(obj) {
if (!obj || (typeof obj.length !== 'number')) return false;
if (obj == null || isWindow(obj)) {
return false;
}

var length = obj.length;

// We have on object which has length property. Should we treat it as array?
if (typeof obj.hasOwnProperty != 'function' &&
typeof obj.constructor != 'function') {
// This is here for IE8: it is a bogus object treat it as array;
if (obj.nodeType === 1 && length) {
return true;
} else {
return obj instanceof JQLite || // JQLite
(jQuery && obj instanceof jQuery) || // jQuery
toString.call(obj) !== '[object Object]' || // some browser native object
typeof obj.callee === 'function'; // arguments (on IE8 looks like regular obj)
}

return isArray(obj) || !isFunction(obj) && (
length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj
);
}

/**
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 @@ -77,7 +77,27 @@ describe('ngRepeat', function() {
expect(element.find('li').length).toEqual(3);
expect(element.text()).toEqual('x;y;x;');
});

it('should iterate over an array-like class', function() {
function Collection() {}
Collection.prototype = new Array();
Collection.prototype.length = 0;

var collection = new Collection();
collection.push({ name: "x" });
collection.push({ name: "y" });
collection.push({ name: "z" });

element = $compile(
'<ul>' +
'<li ng-repeat="item in items">{{item.name}};</li>' +
'</ul>')(scope);

scope.items = collection;
scope.$digest();
expect(element.find('li').length).toEqual(3);
expect(element.text()).toEqual('x;y;z;');
});

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