Skip to content

Commit

Permalink
fix(ngRepeat): handle iteration over identical obj values
Browse files Browse the repository at this point in the history
Modifies default trackByIdFn to factor both key and value into hashKey
for non-array primitive (i.e. index not provided) values

Close angular#2787
  • Loading branch information
worrel committed May 27, 2013
1 parent b8ea7f6 commit 074debb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,16 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
return trackByExpGetter($scope, hashFnLocals);
};
} else {
trackByIdFn = function(key, value) {
return hashKey(value);
trackByIdFn = function(key, value, index) {
var valType = typeof value;

// if value is a primitive and key != index (case for arrays)
// then append key to value to ensure uniqueness for identical values
if(valType != 'object' && !(typeof key === 'number' || key === index)) {
return hashKey(key + ':' + value);
} else {
return hashKey(value);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ describe('ngRepeat', function() {
expect(element.text()).toEqual('misko:swe|shyam:set|');
});

it('should iterate over an object/map with identical values', function() {
element = $compile(
'<ul>' +
'<li ng-repeat="(key, value) in items">{{key}}:{{value}}|</li>' +
'</ul>')(scope);
scope.items = {age:20, wealth:20, prodname: "Bingo", dogname: "Bingo", codename: "20"};
scope.$digest();
expect(element.text()).toEqual('age:20|codename:20|dogname:Bingo|prodname:Bingo|wealth:20|');
});

describe('track by', function() {
it('should track using expression function', function() {
Expand Down

0 comments on commit 074debb

Please sign in to comment.