diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js index 0a2673ffdb6b..ffb37abc2aaa 100644 --- a/src/ng/directive/ngRepeat.js +++ b/src/ng/directive/ngRepeat.js @@ -201,22 +201,29 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { priority: 1000, terminal: true, $$tlb: true, - link: function($scope, $element, $attr, ctrl, $transclude){ - var expression = $attr.ngRepeat; - var match = expression.match(/^\s*(.+)\s+in\s+([\r\n\s\S]*?)\s*(\s+track\s+by\s+(.+)\s*)?$/), - trackByExp, trackByExpGetter, trackByIdExpFn, trackByIdArrayFn, trackByIdObjFn, - lhs, rhs, valueIdentifier, keyIdentifier, - hashFnLocals = {$id: hashKey}; + link: function($scope, $element, $attr, ctrl, $transclude) { + var expression = $attr.ngRepeat, + match = expression.match(/^\s*(.+)\s+in\s+([\r\n\s\S]*?)\s*(\s+track\s+by\s+(.+)\s*)?$/), + lhs, rhs, trackByExp, + valueIdentifier, keyIdentifier, + trackByExpGetter, trackByIdExpFn, trackByIdArrayFn, trackByIdObjFn, + hashFnLocals = {$id: hashKey}; - if (!match) { - throw ngRepeatMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.", - expression); - } + if (!match) throw ngRepeatMinErr('iexp', + "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.", expression); lhs = match[1]; rhs = match[2]; trackByExp = match[4]; + match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/); + + if (!match) throw ngRepeatMinErr('iidexp', + "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.", lhs); + + valueIdentifier = match[3] || match[1]; + keyIdentifier = match[2]; + if (trackByExp) { trackByExpGetter = $parse(trackByExp); trackByIdExpFn = function(key, value, index) { @@ -224,6 +231,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { if (keyIdentifier) hashFnLocals[keyIdentifier] = key; hashFnLocals[valueIdentifier] = value; hashFnLocals.$index = index; + return trackByExpGetter($scope, hashFnLocals); }; } else { @@ -235,14 +243,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { }; } - match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/); - if (!match) { - throw ngRepeatMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.", - lhs); - } - valueIdentifier = match[3] || match[1]; - keyIdentifier = match[2]; - // Store a list of elements from previous run. This is a hash where key is the item from the // iterator, and the value is objects with following properties. // - scope: bound scope