问题描述
我正在使用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.如果需要,可以为文本区域创建一个类似的页面.
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行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!