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

Commit cd9a7b9

Browse files
committed
fix(ng:repeat): support repeating over array with null
typeof null == 'object', but it doesn't behave like an object because its properties can't be dereferenced, so we need to special-case it. Closes #702
1 parent 1dccaaa commit cd9a7b9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/apis.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414
* The resulting string key is in 'type:hashKey' format.
1515
*/
1616
function hashKey(obj) {
17-
var objType = typeof obj;
18-
var key = obj;
19-
if (objType == 'object') {
17+
var objType = typeof obj,
18+
key;
19+
20+
if (objType == 'object' && obj !== null) {
2021
if (typeof (key = obj.$$hashKey) == 'function') {
2122
// must invoke on object to keep the right this
2223
key = obj.$$hashKey();
2324
} else if (key === undefined) {
2425
key = obj.$$hashKey = nextUid();
2526
}
27+
} else {
28+
key = obj;
2629
}
30+
2731
return objType + ':' + key;
2832
}
2933

test/ApiSpecs.js

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ describe('api', function() {
3636
expect(map.shift('key')).toEqual(undefined);
3737
expect(map[hashKey('key')]).toEqual(undefined);
3838
});
39+
40+
it('should support primitive and object keys', function() {
41+
var obj1 = {},
42+
obj2 = {};
43+
44+
var map = new HashQueueMap();
45+
map.push(obj1, 'a1');
46+
map.push(obj1, 'a2');
47+
map.push(obj2, 'b');
48+
map.push(1, 'c');
49+
map.push(undefined, 'd');
50+
map.push(null, 'e');
51+
52+
expect(map[hashKey(obj1)]).toEqual(['a1', 'a2']);
53+
expect(map[hashKey(obj2)]).toEqual(['b']);
54+
expect(map[hashKey(1)]).toEqual(['c']);
55+
expect(map[hashKey(undefined)]).toEqual(['d']);
56+
expect(map[hashKey(null)]).toEqual(['e']);
57+
});
3958
});
4059
});
4160

test/widgetsSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ describe('widget', function() {
429429
expect(element.text()).toBe('a|b|||c||d|');
430430
}));
431431

432+
it('should iterate over all kinds of types', inject(function($rootScope, $compile) {
433+
var element = $compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>')($rootScope);
434+
$rootScope.array = ['a', 1, null, undefined, {}];
435+
$rootScope.$digest();
436+
437+
expect(element.text()).toMatch(/a\|1\|\|\|\{\s*\}\|/);
438+
}));
439+
432440

433441
describe('stability', function() {
434442
var a, b, c, d, lis, element;

0 commit comments

Comments
 (0)