angularjs-trim()函数不起作用
这是我的js代码,用于删除字符串和修剪字符串中的多个空格:angularjs-trim()函数不起作用,angularjs,input,trim,Angularjs,Input,Trim,这是我的js代码,用于删除字符串和修剪字符串中的多个空格: $scope.fixInput = function (input){ input = input.replace(/\s+/g, ' ').trim(); return input; }; 我有html中的输入字段: <input type="text" ng-model="userName" ng-change="userName=fixInput(userName)"/> 结果会很好的 “a” 但是
$scope.fixInput = function (input){
input = input.replace(/\s+/g, ' ').trim();
return input;
};
我有html中的输入字段:
<input type="text" ng-model="userName" ng-change="userName=fixInput(userName)"/>
结果会很好的
“a”
但是当我打字的时候
" aaa "
当只需要修剪字符串时,结果不会改变,是一样的 角度自动修剪
输入[文本]
。如果要手动执行,必须禁用它:
<input type="text" ng-model="userName" ng-trim="false" ng-change="userName=fixInput(userName)" />
^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^
发件人:
如果设置为false,AngularJS将不会自动修剪输入。这
输入[type=password]控件的参数被忽略,这将
永远不要修剪输入
(默认值:true)
在模型更改时更改模型,这难道不是很危险吗(例如无限循环)?我刚刚在浏览器控制台
var v=“aaa”使用了这个代码片段;v、 替换(/\s+/g')。修剪()
并发现输出为aaa
。“那么这很好,还是你在期待别的什么呢?”阿披实马拉卡我也看到了,这让我很困惑。但是input中的值没有改变。我只是在angular上下文中尝试了一下,发现一个错误,input.replace不是一个函数抱歉,我的错误。它给我提供了预期的结果var-app=angular.module(“test”,[]).controller(“TestController”,function($http,$scope){var-vm=this;vm.my=true;$scope.fixInput=function(input){console.log(input);input=input.replace(/\s+//g',).trim();return input;})代码>我试过了,我去掉了修剪功能,现在我得到了我想要的,我在开始和结束时只有一个空间需要修剪
<input type="text" ng-model="userName" ng-trim="false" ng-change="userName=fixInput(userName)" />
^^^^^^^^^^^^^^^