@@ -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) {
370378function 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 ( ) {
0 commit comments