Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c903080

Browse files
grevorymhevery
authored andcommitted
feat(ngModel) support ngNoTrim attribute
1 parent c711f3f commit c903080

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/ng/directive/input.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ 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=} ngNoTrim Prevents Angular from automatically trimming the value to contain no
29+
* extra white space
2830
*
2931
* @example
3032
<doc:example>
@@ -37,7 +39,7 @@ var inputType = {
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-no-trim>
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,15 @@ 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-no-trim exists we will avoid trimming
385+
// e.g. <input ng-model="foo" ng-no-trim>
386+
if (isUndefined(attr.ngNoTrim)) {
387+
var value = trim(value);
388+
}
389+
374390

375391
if (ctrl.$viewValue !== value) {
376392
scope.$apply(function() {

test/ng/directive/inputSpec.js

Lines changed: 8 additions & 0 deletions
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-no-trim/>');
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)