我发现了一些奇怪的行为:默认情况下角度修剪模型值。而快速搜索谷歌并不能帮助我解决这个问题。我找到了ng-no-trim
指令提案、ng-trim
等等。但是什么都不起作用。
我在下面提供了一个代表这个问题的小片段。
function Ctrl($scope) {
$scope.text='';
$scope.$watch('text', function (newValue) {
console.log(newValue);
});
}
您也可以尝试这个代码片段here。
我已经添加了一个与模型text
同步的文本区。但当添加新的尾随空格或将行断开到新的空格时,它不会对观看做出反应。
我可以做些什么来关闭这种行为?谢谢。
发布于 2013-02-19 21:26:50
这个指令是1.1.1中的新指令;您可以使用JS Bin snippet查看它的工作情况。
<textarea cols="30" rows="10" ng-model="text" ng-trim="false"></textarea>
发布于 2014-05-13 01:22:18
angular 1.0.x的回退
var app = angular.module('app', []);
app.directive('ngTrim', function() {
return {
require: 'ngModel',
priority: 300,
link: function(scope, iElem, iAttrs, ngModel) {
if (iAttrs.ngTrim === 'false') {
// Be careful here. We override any value comming from the previous
// parsers to return the real value in iElem
ngModel.$parsers.unshift(function() {
return iElem.val();
});
}
}
}
});
angular.bootstrap(document, ['app']);
发布于 2016-04-04 02:39:30
您可以使用ng- trim =true/false启用/禁用trim选项。参考https://docs.angularjs.org/api/ng/input/input%5Btext%5D
https://stackoverflow.com/questions/14958418
复制相似问题