diff options
| author | Gregory Pike | 2012-08-31 19:20:01 -0400 | 
|---|---|---|
| committer | Misko Hevery | 2012-09-06 16:06:23 -0700 | 
| commit | d519953a4b219035587e3fcb2e9cc52e02b408ca (patch) | |
| tree | cf4e66c0885bce26745000d324db6204cd73b37e /src/ng/directive/input.js | |
| parent | 4909d1d39d61d6945a0820a5a7276c1e657ba262 (diff) | |
| download | angular.js-d519953a4b219035587e3fcb2e9cc52e02b408ca.tar.bz2 | |
feat(ngModel): support ngTrim attribute on input
Diffstat (limited to 'src/ng/directive/input.js')
| -rw-r--r-- | src/ng/directive/input.js | 21 | 
1 files changed, 18 insertions, 3 deletions
| diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index cd2f0cfc..412d768d 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -25,6 +25,8 @@ var inputType = {     *    patterns defined as scope expressions.     * @param {string=} ngChange Angular expression to be executed when input changes due to user     *    interaction with the input element. +   * @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trimming the +   *    input.     *     * @example        <doc:example> @@ -32,12 +34,12 @@ var inputType = {           <script>             function Ctrl($scope) {               $scope.text = 'guest'; -             $scope.word = /^\w*$/; +             $scope.word = /^\s*\w*\s*$/;             }           </script>           <form name="myForm" ng-controller="Ctrl">             Single word: <input type="text" name="input" ng-model="text" -                               ng-pattern="word" required> +                               ng-pattern="word" required ng-trim="false">             <span class="error" ng-show="myForm.input.$error.required">               Required!</span>             <span class="error" ng-show="myForm.input.$error.pattern"> @@ -66,6 +68,12 @@ var inputType = {              input('text').enter('hello world');              expect(binding('myForm.input.$valid')).toEqual('false');            }); + +          it('should not be trimmed', function() { +            input('text').enter('untrimmed '); +            expect(binding('text')).toEqual('untrimmed '); +            expect(binding('myForm.input.$valid')).toEqual('true'); +          });          </doc:scenario>        </doc:example>     */ @@ -370,7 +378,14 @@ function isEmpty(value) {  function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {    var listener = function() { -    var value = trim(element.val()); +    var value = element.val(); + +    // By default we will trim the value +    // If the attribute ng-trim exists we will avoid trimming +    // e.g. <input ng-model="foo" ng-trim="false"> +    if (toBoolean(attr.ngTrim || 'T')) { +      value = trim(value); +    }      if (ctrl.$viewValue !== value) {        scope.$apply(function() { | 
