mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-14 01:33:11 +00:00
feat(ng-list): Allow custom separator
This commit is contained in:
parent
7b52586f7c
commit
13f31602f3
2 changed files with 29 additions and 2 deletions
|
|
@ -1101,6 +1101,8 @@ var requiredDirective = [function() {
|
||||||
* Text input that converts between comma-seperated string into an array of strings.
|
* Text input that converts between comma-seperated string into an array of strings.
|
||||||
*
|
*
|
||||||
* @element input
|
* @element input
|
||||||
|
* @param {string=} ng-list 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.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
<doc:example>
|
<doc:example>
|
||||||
|
|
@ -1139,12 +1141,15 @@ var ngListDirective = function() {
|
||||||
return {
|
return {
|
||||||
require: 'ngModel',
|
require: 'ngModel',
|
||||||
link: function(scope, element, attr, ctrl) {
|
link: function(scope, element, attr, ctrl) {
|
||||||
|
var match = /\/(.*)\//.exec(attr.ngList),
|
||||||
|
separator = match && new RegExp(match[1]) || attr.ngList || ',';
|
||||||
|
|
||||||
var parse = function(viewValue) {
|
var parse = function(viewValue) {
|
||||||
var list = [];
|
var list = [];
|
||||||
|
|
||||||
if (viewValue) {
|
if (viewValue) {
|
||||||
forEach(viewValue.split(/\s*,\s*/), function(value) {
|
forEach(viewValue.split(separator), function(value) {
|
||||||
if (value) list.push(value);
|
if (value) list.push(trim(value));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -871,6 +871,28 @@ describe('input', function() {
|
||||||
changeInputValueTo('');
|
changeInputValueTo('');
|
||||||
expect(scope.list).toEqual([]);
|
expect(scope.list).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should allow custom separator', function() {
|
||||||
|
compileInput('<input type="text" ng-model="list" ng-list=":" />');
|
||||||
|
|
||||||
|
changeInputValueTo('a,a');
|
||||||
|
expect(scope.list).toEqual(['a,a']);
|
||||||
|
|
||||||
|
changeInputValueTo('a:b');
|
||||||
|
expect(scope.list).toEqual(['a', 'b']);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should allow regexp as a separator', function() {
|
||||||
|
compileInput('<input type="text" ng-model="list" ng-list="/:|,/" />');
|
||||||
|
|
||||||
|
changeInputValueTo('a,b');
|
||||||
|
expect(scope.list).toEqual(['a', 'b']);
|
||||||
|
|
||||||
|
changeInputValueTo('a,b: c');
|
||||||
|
expect(scope.list).toEqual(['a', 'b', 'c']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('required', function() {
|
describe('required', function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue