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

Commit c9705b7

Browse files
Gias Kay LeeIgorMinar
Gias Kay Lee
authored andcommitted
fix(ngRepeat): allow for more flexible coding style in ngRepeat expression
With this change it's possible to split the ng-repeat expression into multiple lines at any point in the expression where white-space is expected. Closes #5537 Closes #5598
1 parent 53ec33f commit c9705b7

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/ng/directive/ngRepeat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
203203
$$tlb: true,
204204
link: function($scope, $element, $attr, ctrl, $transclude){
205205
var expression = $attr.ngRepeat;
206-
var match = expression.match(/^\s*(.+)\s+in\s+([\r\n\s\S]*?)\s*(\s+track\s+by\s+(.+)\s*)?$/),
206+
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?\s*$/),
207207
trackByExp, trackByExpGetter, trackByIdExpFn, trackByIdArrayFn, trackByIdObjFn,
208208
lhs, rhs, valueIdentifier, keyIdentifier,
209209
hashFnLocals = {$id: hashKey};
@@ -215,7 +215,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
215215

216216
lhs = match[1];
217217
rhs = match[2];
218-
trackByExp = match[4];
218+
trackByExp = match[3];
219219

220220
if (trackByExp) {
221221
trackByExpGetter = $parse(trackByExp);

test/ng/directive/ngRepeatSpec.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,6 @@ describe('ngRepeat', function() {
177177
});
178178

179179

180-
it('should allow expressions over multiple lines', function() {
181-
scope.isTrue = function() {
182-
return true;
183-
};
184-
element = $compile(
185-
'<ul>' +
186-
'<li ng-repeat="item in items\n' +
187-
'| filter:isTrue">{{item.name}}</li>' +
188-
'</ul>')(scope);
189-
scope.items = [{name: 'igor'}];
190-
scope.$digest();
191-
192-
expect(element.find('li').text()).toBe('igor');
193-
});
194-
195-
196180
it('should track using provided function when a filter is present', function() {
197181
scope.newArray = function (items) {
198182
var newArray = [];
@@ -354,6 +338,38 @@ describe('ngRepeat', function() {
354338
});
355339

356340

341+
it('should allow expressions over multiple lines', function() {
342+
element = $compile(
343+
'<ul>' +
344+
'<li ng-repeat="item in items\n' +
345+
'| filter:isTrue">{{item.name}}/</li>' +
346+
'</ul>')(scope);
347+
348+
scope.isTrue = function() {return true;};
349+
scope.items = [{name: 'igor'}, {name: 'misko'}];
350+
351+
scope.$digest();
352+
353+
expect(element.text()).toEqual('igor/misko/');
354+
});
355+
356+
357+
it('should strip white space characters correctly', function() {
358+
element = $compile(
359+
'<ul>' +
360+
'<li ng-repeat="item \t\n \t in \n \t\n\n \nitems \t\t\n | filter:\n\n{' +
361+
'\n\t name:\n\n \'ko\'\n\n}\n\n | orderBy: \t \n \'name\' \n\n' +
362+
'track \t\n by \n\n\t $index \t\n ">{{item.name}}/</li>' +
363+
'</ul>')(scope);
364+
365+
scope.items = [{name: 'igor'}, {name: 'misko'}];
366+
367+
scope.$digest();
368+
369+
expect(element.text()).toEqual('misko/');
370+
});
371+
372+
357373
it('should not ngRepeat over parent properties', function() {
358374
var Class = function() {};
359375
Class.prototype.abc = function() {};

0 commit comments

Comments
 (0)