更改 AngularJS ngTrim 行为 [英] Change AngularJS ngTrim Behavior
问题描述
我使用的是 AngularJS 1.5.6 版.我有一个包含大量文本区域和文本输入的大型应用程序.我今天发现了一个错误,这是由 AngularJS 为文本类型输入修剪输入的默认行为引起的.我想将此行为从默认修剪更改为默认不修剪.
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?)
这是描述 ng-trim (ngTrim) 默认行为的文档页面.
Here is the documentation page describing the default behavior of ng-trim (ngTrim).
https://docs.angularjs.org/api/ng/input/input%5Btext%5D
推荐答案
以下指令应该适用于所有输入 type=text 字段.默认情况下,它将为每个输入字段设置 ngTrim false 的值.如果需要,可以为 textarea 创建一个类似的.
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');
}
}
};
});
这篇关于更改 AngularJS ngTrim 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!