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

Commit d519953

Browse files
grevorymhevery
authored andcommitted
feat(ngModel): support ngTrim attribute on input
1 parent 4909d1d commit d519953

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/ng/directive/input.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ var inputType = {
2525
* patterns defined as scope expressions.
2626
* @param {string=} ngChange Angular expression to be executed when input changes due to user
2727
* interaction with the input element.
28+
* @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trimming the
29+
* input.
2830
*
2931
* @example
3032
<doc:example>
3133
<doc:source>
3234
<script>
3335
function Ctrl($scope) {
3436
$scope.text = 'guest';
35-
$scope.word = /^\w*$/;
37+
$scope.word = /^\s*\w*\s*$/;
3638
}
3739
</script>
3840
<form name="myForm" ng-controller="Ctrl">
3941
Single word: <input type="text" name="input" ng-model="text"
40-
ng-pattern="word" required>
42+
ng-pattern="word" required ng-trim="false">
4143
<span class="error" ng-show="myForm.input.$error.required">
4244
Required!</span>
4345
<span class="error" ng-show="myForm.input.$error.pattern">
@@ -66,6 +68,12 @@ var inputType = {
6668
input('text').enter('hello world');
6769
expect(binding('myForm.input.$valid')).toEqual('false');
6870
});
71+
72+
it('should not be trimmed', function() {
73+
input('text').enter('untrimmed ');
74+
expect(binding('text')).toEqual('untrimmed ');
75+
expect(binding('myForm.input.$valid')).toEqual('true');
76+
});
6977
</doc:scenario>
7078
</doc:example>
7179
*/
@@ -370,7 +378,14 @@ function isEmpty(value) {
370378
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
371379

372380
var listener = function() {
373-
var value = trim(element.val());
381+
var value = element.val();
382+
383+
// By default we will trim the value
384+
// If the attribute ng-trim exists we will avoid trimming
385+
// e.g. <input ng-model="foo" ng-trim="false">
386+
if (toBoolean(attr.ngTrim || 'T')) {
387+
value = trim(value);
388+
}
374389

375390
if (ctrl.$viewValue !== value) {
376391
scope.$apply(function() {

test/ng/directive/inputSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ describe('input', function() {
382382
});
383383

384384

385+
it('should update the model and not trim the value', function() {
386+
compileInput('<input type="text" ng-model="name" name="alias" ng-trim="false" />');
387+
388+
changeInputValueTo(' a ');
389+
expect(scope.name).toEqual(' a ');
390+
});
391+
392+
385393
it('should allow complex reference binding', function() {
386394
compileInput('<input type="text" ng-model="obj[\'abc\'].name"/>');
387395

0 commit comments

Comments
 (0)