Skip to content

Commit

Permalink
fix(ngRepeat): allow for more flexible coding style in ngRepeat expre…
Browse files Browse the repository at this point in the history
…ssion

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 angular#5537
Closes angular#5598
  • Loading branch information
Gias Kay Lee authored and jamesdaily committed Jan 27, 2014
1 parent 941c534 commit fbc60d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
$$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*)?$/),
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?\s*$/),
trackByExp, trackByExpGetter, trackByIdExpFn, trackByIdArrayFn, trackByIdObjFn,
lhs, rhs, valueIdentifier, keyIdentifier,
hashFnLocals = {$id: hashKey};
Expand All @@ -215,7 +215,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {

lhs = match[1];
rhs = match[2];
trackByExp = match[4];
trackByExp = match[3];

if (trackByExp) {
trackByExpGetter = $parse(trackByExp);
Expand Down
48 changes: 32 additions & 16 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,6 @@ describe('ngRepeat', function() {
});


it('should allow expressions over multiple lines', function() {
scope.isTrue = function() {
return true;
};
element = $compile(
'<ul>' +
'<li ng-repeat="item in items\n' +
'| filter:isTrue">{{item.name}}</li>' +
'</ul>')(scope);
scope.items = [{name: 'igor'}];
scope.$digest();

expect(element.find('li').text()).toBe('igor');
});


it('should track using provided function when a filter is present', function() {
scope.newArray = function (items) {
var newArray = [];
Expand Down Expand Up @@ -354,6 +338,38 @@ describe('ngRepeat', function() {
});


it('should allow expressions over multiple lines', function() {
element = $compile(
'<ul>' +
'<li ng-repeat="item in items\n' +
'| filter:isTrue">{{item.name}}/</li>' +
'</ul>')(scope);

scope.isTrue = function() {return true;};
scope.items = [{name: 'igor'}, {name: 'misko'}];

scope.$digest();

expect(element.text()).toEqual('igor/misko/');
});


it('should strip white space characters correctly', function() {
element = $compile(
'<ul>' +
'<li ng-repeat="item \t\n \t in \n \t\n\n \nitems \t\t\n | filter:\n\n{' +
'\n\t name:\n\n \'ko\'\n\n}\n\n | orderBy: \t \n \'name\' \n\n' +
'track \t\n by \n\n\t $index \t\n ">{{item.name}}/</li>' +
'</ul>')(scope);

scope.items = [{name: 'igor'}, {name: 'misko'}];

scope.$digest();

expect(element.text()).toEqual('misko/');
});


it('should not ngRepeat over parent properties', function() {
var Class = function() {};
Class.prototype.abc = function() {};
Expand Down

0 comments on commit fbc60d2

Please sign in to comment.