How prevent angular version earlier than 1.3.0 auto trim for fields?

Is there any way prevent angular version earlier 1.3.0 from auto trim for fields in the whole application? I know that I can prevent it for specified field using ngTrim directive, but it doesn't look good add this directive to all text fields in the application, is there any way do it for all fields in the angular module? Here is code, if you add add spaces in the begin of input they will not appear in label:

<div ng-app>
  <div ng-controller="TodoCtrl">
      {{field}}
    <input type="text" ng-model="field">
  </div>
</div>

Also here is link for my first question How prevent angular auto trim for fields?, the answer there is working for angular 1.3.0 or later versions.

Answers:

Answer

In Angular prior to 1.3.x you can try to decorate compile function:

app.config(function($provide) {
    $provide.decorator('inputDirective', function($delegate) {

        var directive = $delegate[0];

        directive.compile = function(orig) {
            return function(element, attrs, transclude) {
                attrs.$set('ngTrim', 'false');
                return orig.apply(null, arguments);
            };
        }(directive.compile);

        return $delegate;
    });
});

This approach should also work in 1.3.x though.

Demo: http://plnkr.co/edit/gvIa7omiFWwoWW9tyl4S?p=preview

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.