@@ -25,19 +25,21 @@ var inputType = {
25
25
* patterns defined as scope expressions.
26
26
* @param {string= } ngChange Angular expression to be executed when input changes due to user
27
27
* interaction with the input element.
28
+ * @param {boolean= } [ngTrim=true] If set to false Angular will not automatically trimming the
29
+ * input.
28
30
*
29
31
* @example
30
32
<doc:example>
31
33
<doc:source>
32
34
<script>
33
35
function Ctrl($scope) {
34
36
$scope.text = 'guest';
35
- $scope.word = /^\w *$/;
37
+ $scope.word = /^\s*\w*\s *$/;
36
38
}
37
39
</script>
38
40
<form name="myForm" ng-controller="Ctrl">
39
41
Single word: <input type="text" name="input" ng-model="text"
40
- ng-pattern="word" required>
42
+ ng-pattern="word" required ng-trim="false" >
41
43
<span class="error" ng-show="myForm.input.$error.required">
42
44
Required!</span>
43
45
<span class="error" ng-show="myForm.input.$error.pattern">
@@ -66,6 +68,12 @@ var inputType = {
66
68
input('text').enter('hello world');
67
69
expect(binding('myForm.input.$valid')).toEqual('false');
68
70
});
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
+ });
69
77
</doc:scenario>
70
78
</doc:example>
71
79
*/
@@ -370,7 +378,14 @@ function isEmpty(value) {
370
378
function textInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
371
379
372
380
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
+ }
374
389
375
390
if ( ctrl . $viewValue !== value ) {
376
391
scope . $apply ( function ( ) {
0 commit comments