[英]Format input value in Angularjs
我正在尝试编写一个指令来自动格式化<input>
的数字,但模型没有格式化。 让它工作很好,加载时输入中的值在控制器中显示为 1,000,000 和 1000000,但是当只输入ngModel.$parsers
函数时会触发。 ngModel.$formatters
触发的唯一时间是指令被加载并且值为 0 时。
我怎样才能让它在 keypress 上工作(我已经尝试绑定到 keypress/keyup 但它不起作用)。
这是我的代码:
angular.module('myApp.directives', []).directive('filterInput', ['$filter', function($filter) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
ngModel.$parsers.push(function fromUser(text) {
return parseInt(text.replace(",", ""));
});
ngModel.$formatters.push(function toUser(text) {
console.log($filter('number')(text));
return ($filter('number')(text || ''));
});
}
};
}]);
这是我们使用unshift
工作示例:
angular.module('myApp.directives', []).directive('format', ['$filter', function ($filter) {
return {
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
return $filter(attrs.format)(ctrl.$modelValue)
});
ctrl.$parsers.unshift(function (viewValue) {
var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
elem.val($filter(attrs.format)(plainNumber));
return plainNumber;
});
}
};
}]);
HTML似乎:
<input type="text" ng-model="test" format="number" />
见演示小提琴
希望它的帮助
这个模块对我来说很好用。
https://github.com/assisrafael/angular-input-masks
例子:
<input type="text" name="field" ng-model="number" ui-number-mask>
根据对这个问题的回答,对下面的 Maxim Shoustin 的回答进行了小编辑: AngularJS 格式化程序 - 如何显示空白而不是零
唯一的变化是确保在删除最后一个数字时输入为空白而不是零:
ctrl.$parsers.unshift(function (viewValue) {
console.log(viewValue);
if(viewValue){
var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
elem.val($filter('number')(plainNumber));
return plainNumber;
}else{
return '';
}
});
我为自己创建了这个指令解决方案,它可以:
推荐:使用 [maxLength] 将用户限制为一定长度。
<input [maxLength]="9" appPriceUsd>
这是指令:
// Format USD by Reza Taba
import { DecimalPipe } from '@angular/common';
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appPriceUsd]'
})
export class PriceUsdDirective {
constructor(private elRef: ElementRef, private decimalPipe: DecimalPipe) { }
@HostListener('focus') initializeValue(): void {
if (this.elRef.nativeElement.value === '') {
this.elRef.nativeElement.value = '0.00';
}
}
@HostListener('keyup') formatUsd(): void {
let value: string;
value = this.elRef.nativeElement.value as string;
value = this.removeNonDigtis(value); // remove all non-digit values
value = this.addDecimalPoint(value); // Add . to the -2 index
value = this.applyDecimalPipe(value); // to divide every thousand
this.elRef.nativeElement.value = value;
}
removeNonDigtis(value: string): string {
let inputArray: string[] = [];
const digitArray: string[] = [];
// 12a34b to ["1", "2", "a", "3", "4", "b"]
inputArray = value.split('');
// remove any non-digit value
for (const iterator of inputArray) {
if (/[0-9]/.test(iterator)) {
digitArray.push(iterator);
}
}
return digitArray.join('');
}
addDecimalPoint(value: string): string {
const inputArray = value.split(''); // ['0', '.', '0', '0']
inputArray.splice(-2, 0, '.'); // place decimal in -2
return inputArray.join('');
}
applyDecimalPipe(value: string): string {
console.log(value);
return value === '' || value === '.'
? '0.00'
: this.decimalPipe.transform(value, '1.2-2');
}
}
希望能帮助到你。 享受编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.