-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngList) Fix model to view rendering when using a custom separator #2561
Changes from all commits
e99194d
290c4b6
99ad93b
d04d3c7
72b75ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1225,6 +1225,8 @@ var requiredDirective = function() { | |
* @element input | ||
* @param {string=} ngList optional delimiter that should be used to split the value. If | ||
* specified in form `/something/` then the value will be converted into a regular expression. | ||
* @param {string=} ngListJoin optional string to use when joining elements of the array. | ||
* This is especially useful when the delimeter is a regular expression. The default value is ', '. | ||
* | ||
* @example | ||
<doc:example> | ||
|
@@ -1263,8 +1265,20 @@ var ngListDirective = function() { | |
return { | ||
require: 'ngModel', | ||
link: function(scope, element, attr, ctrl) { | ||
var match = /\/(.*)\//.exec(attr.ngList), | ||
separator = match && new RegExp(match[1]) || attr.ngList || ','; | ||
// Get the attribute values directly from the element rather than the | ||
// attr map otherwise the attribute values will be trimmed of whitespace | ||
var separatorAttribute = element[0].getAttribute(attr.$attr['ngList']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be: attr.ngList not attr.$attr.ngList There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the discussion on whitespace above for why I use getAttribute here. |
||
var joinAttribute = element[0].getAttribute(attr.$attr['ngListJoin']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
var separator, joinedby; | ||
var match = /\/(.*)\//.exec(separatorAttribute); | ||
if (match) { | ||
// This is a regex pattern - always use ', ' to join elements when ngListJoin is undefined | ||
separator = new RegExp(match[1]) | ||
joinedby = joinAttribute || ', '; | ||
}else{ | ||
separator = separatorAttribute || ',' | ||
joinedby = joinAttribute || separatorAttribute || ', '; | ||
} | ||
|
||
var parse = function(viewValue) { | ||
var list = []; | ||
|
@@ -1281,7 +1295,7 @@ var ngListDirective = function() { | |
ctrl.$parsers.push(parse); | ||
ctrl.$formatters.push(function(value) { | ||
if (isArray(value)) { | ||
return value.join(', '); | ||
return value.join(joinedby); | ||
} | ||
|
||
return undefined; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1009,9 +1009,62 @@ describe('input', function() { | |
it('should allow custom separator', function() { | ||
compileInput('<input type="text" ng-model="list" ng-list=":" />'); | ||
|
||
changeInputValueTo('a,a'); | ||
expect(scope.list).toEqual(['a,a']); | ||
// model -> view | ||
scope.$apply(function() { | ||
scope.list = ['x', 'y', 'z']; | ||
}); | ||
expect(inputElm.val()).toBe('x:y:z'); //Should use the separator as the join string | ||
|
||
// view -> model | ||
changeInputValueTo('a:b'); | ||
expect(scope.list).toEqual(['a', 'b']); | ||
}); | ||
|
||
|
||
it('should allow custom separator with whitespace', function() { | ||
compileInput('<input type="text" ng-model="list" ng-list=" or " />'); | ||
|
||
changeInputValueTo('House or Door or Chair'); | ||
expect(scope.list).toEqual(['House', 'Door', 'Chair']); | ||
}); | ||
|
||
|
||
it('should allow custom join string', function() { | ||
compileInput('<input type="text" ng-model="list" ng-list ng-list-join=" and "/>'); | ||
|
||
// model -> view | ||
scope.$apply(function() { | ||
scope.list = ['x', 'y', 'z']; | ||
}); | ||
expect(inputElm.val()).toBe('x and y and z'); | ||
}); | ||
|
||
|
||
it('should allow custom separator and custom join string', function() { | ||
compileInput('<input type="text" ng-model="list" ng-list=":" ng-list-join=" and "/>'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes no sense. How can you split on ':' and join on " and ". This means that during edit the text would change on you if someone changes the model. This is undesirable. I really think that there should be on ng-list and and some intelligent logic about how to ignore whitespace. So if it is work white space is added, but otherwise it is ignored or something like that. |
||
|
||
// model -> view | ||
scope.$apply(function() { | ||
scope.list = ['x', 'y', 'z']; | ||
}); | ||
expect(inputElm.val()).toBe('x and y and z'); | ||
|
||
// view -> model | ||
changeInputValueTo('a:b'); | ||
expect(scope.list).toEqual(['a', 'b']); | ||
}); | ||
|
||
|
||
it('should allow custom separator and custom join string in alternative attribute form', function() { | ||
compileInput('<input type="text" ng-model="list" ng:list=":" data-ng-list-join=" and "/>'); | ||
|
||
// model -> view | ||
scope.$apply(function() { | ||
scope.list = ['x', 'y', 'z']; | ||
}); | ||
expect(inputElm.val()).toBe('x and y and z'); | ||
|
||
// view -> model | ||
changeInputValueTo('a:b'); | ||
expect(scope.list).toEqual(['a', 'b']); | ||
}); | ||
|
@@ -1020,12 +1073,30 @@ describe('input', function() { | |
it('should allow regexp as a separator', function() { | ||
compileInput('<input type="text" ng-model="list" ng-list="/:|,/" />'); | ||
|
||
// model -> view | ||
scope.$apply(function() { | ||
scope.list = ['x', 'y', 'z']; | ||
}); | ||
expect(inputElm.val()).toBe('x, y, z'); //Should use ', ' as the default join string | ||
|
||
// view -> model | ||
changeInputValueTo('a,b'); | ||
expect(scope.list).toEqual(['a', 'b']); | ||
|
||
changeInputValueTo('a,b: c'); | ||
expect(scope.list).toEqual(['a', 'b', 'c']); | ||
}); | ||
|
||
|
||
it('should allow regexp as a separator and custom join string', function() { | ||
compileInput('<input type="text" ng-model="list" ng-list="/:|,/" ng-list-join="; " />'); | ||
|
||
// model -> view | ||
scope.$apply(function() { | ||
scope.list = ['x', 'y', 'z']; | ||
}); | ||
expect(inputElm.val()).toBe('x; y; z'); | ||
}); | ||
}); | ||
|
||
describe('required', function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be just trim of the ngList. You can not have different join and separator values.