Change AngularJS ngTrim Behavior

房东的猫 提交于 2019-12-10 23:07:39

问题


I am using AngularJS version 1.5.6. I have a large application with lots of text areas and text inputs. I discovered a bug today caused by the default AngularJS behavior of trimming input for text type inputs. I would like to change this behavior from trimming by default to not trimming by default.

Is there an easy way to do this rather than to go through hundreds of textareas and text inputs in my application. (perhaps globally or writing my own directive?)

Here is the documentation page describing the default behavior of ng-trim (ngTrim).

https://docs.angularjs.org/api/ng/input/input%5Btext%5D


回答1:


The below directive should work for all the input type=text fields. It will set the value of ngTrim false by default for every input field. And a similar one can be created for the textarea if required.

.directive('input', function($compile){
   return {
   link(scope, element, attrs) {
    if (element.attr('type') === 'text') {
      attrs.$set('ngTrim', 'false');
    }
  }
  };
 });


来源:https://stackoverflow.com/questions/45309473/change-angularjs-ngtrim-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!