2 回答
TA贡献1817条经验 获得超6个赞
我会做这样的事情:
<div ng-controller="SplitCtrl">
<div class="input-group mb-3">
<input type="text" ng-model="text" ng-keyup="format($event)">
</div>
</div>
var app = angular.module('myApp', []);
app.controller('SplitCtrl',['$scope' ,function ($scope) {
$scope.format = function(e) {
if ($scope.text.length > 1) {
var text = $scope.text.split('').filter(function(_, i) {
return (i % 2) === 0;//filter out previous commas
}).join('') + e.key;
$scope.text = text.split('').join(',');
}
}
}])
TA贡献1826条经验 获得超6个赞
只要您不需要具有多个字符的数字(例如 10 个),这应该可以。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="//code.angularjs.org/1.8.0/angular.min.js"></script>
</head>
<body ng-app="myApp">
<script>
angular.module('myApp', [])
.controller('AppController', ['$scope', function($scope) {
$scope.change = function() {
$scope.numbers = $scope.numbers.replace(/,/g, '').split('').join(',');
};
}]);
</script>
<div ng-controller="AppController">
<input type="text" ng-model="numbers" ng-change="change()" />
</div>
</body>
</html>
添加回答
举报