我使用角度4作为我的前端。我想在输入字段中插入一个千位分隔符。我试过这段代码,但它不能正常工作
<input type="number" class="form-control" id="amount" placeholder="Amount" name="Amount" [ngModel]="Amount | number:'3.2-2'" (keypress)="onlyNumberKey($event)">
&#13;
这是我的component.ts代码
export class CustomerDepositApplicationComponent implements OnInit {
Amount: any;
&#13;
请帮帮我..
答案 0 :(得分:2)
你无法添加这样的管道。您需要做的是,在 ts 中正确格式化您的号码并绑定它。
对于这种情况,numeraljs是一个非常好的库。
让我用它创建一个stackblitz。
<强>更新强>
我创建了简单的stackblitz。请看一看。由于stackblitz需要问题,我无法将数字包导入stackblitz。但如果使用数字,这会更容易。
在此示例中,我使用了.toLocaleString();
功能,其中JS自动检测浏览器区域并适当地转换您的号码。例如,如果您的浏览器区域设置为 en-US ,则数字格式将为15,000。如果它是nb-NO号可能看起来不同。
答案 1 :(得分:0)
您可以添加ngx-currency模块并按以下方式使用它:
<input id="valueInput" class="input amount" placeholder="Amount" [(ngModel)]="amount" value="0" currencyMask min="0" (ngModelChange)="amountChanged($event)" >
答案 2 :(得分:0)
我有类似的问题。为了解决这个问题,我使用了这个article。我创建了一个略微修改的版本以满足我的需求。
import { Directive, ElementRef, forwardRef, HostListener, Input, OnDestroy } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MAT_INPUT_VALUE_ACCESSOR } from '@angular/material';
import { Subscription } from 'rxjs';
import { formatNumber } from '@angular/common';
@Directive({
selector: 'input[localizedNumericInput]',
providers: [
{ provide: MAT_INPUT_VALUE_ACCESSOR, useExisting: LocalizedNumericInputDirective },
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => LocalizedNumericInputDirective),
multi: true
}
]
})
export class LocalizedNumericInputDirective implements ControlValueAccessor, OnDestroy {
locale = 'en';
decimalMarker: string;
constructor(private element: ElementRef<HTMLInputElement>) {
}
private _value: string | null;
get value(): string | null {
return this._value;
}
@Input('value')
set value(value: string | null) {
this._value = value;
this.formatValue(value);
}
@HostListener('input', ['$event.target.value'])
input(value) {
//Find all numerics, decimal marker(, or .) and -
//It will delete thousandSeparator cos it's always opposite to decimal marker
const regExp = new RegExp(`[^\\d${this.decimalMarker}-]`, 'g');
//Separate value on before and after decimal marker
const [integer, decimal] = value.replace(regExp, '').split(this.decimalMarker);
//Send non localized value, with dot as decimalMarker to API
this._value = decimal ? integer.concat('.', decimal) : integer;
// If decimal separator is last character don't update
// because it will delete . || ,
if (this.isLastCharacterDecimalSeparator(value)) {
this._value = value;
}
// here to notify Angular Validators
this._onChange(this._value);
}
@HostListener('blur')
_onBlur() {
/**
* Adding thousand separators
*/
this.formatValue(this._value);
}
@HostListener('focus')
onFocus() {
this.unFormatValue();
}
_onChange(value: any): void {}
/**
* @param value
* apply formatting on value assignment
*/
writeValue(value: any) {
this._value = value;
this.formatValue(this._value);
}
registerOnChange(fn: (value: any) => void) {
this._onChange = fn;
}
registerOnTouched() {}
isLastCharacterDecimalSeparator(value: any) {
return isNaN(value[value.length - 1]);
}
private formatValue(value: string | null) {
if (value === null) {
this.element.nativeElement.value = '';
return;
}
if (this.isLastCharacterDecimalSeparator(value)) {
this.element.nativeElement.value = value;
return;
}
// Conclude the decimal and thousand separators from locale
const [thousandSeparator, decimalMarker] = formatNumber(1000.99, this.locale).replace(/\d/g, '');
this.decimalMarker = decimalMarker;
//Here value should always come with . as decimal marker thus any other behavior is bug
const [integer, decimal] = value.split('.');
//Group every three elements, and add thousandSeparator after them
this.element.nativeElement.value = integer.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
//Add decimals and decimalMarker if any
if (decimal) {
this.element.nativeElement.value = this.element.nativeElement.value.concat(decimalMarker, decimal);
}
}
private unFormatValue() {
const value = this.element.nativeElement.value;
if (this.isLastCharacterDecimalSeparator(value)) {
return;
}
const regExp = new RegExp(`[^\\d${this.decimalMarker}-]`, 'g');
const [integer, decimal] = value.replace(regExp, '').split(this.decimalMarker);
this._value = integer.concat('.', decimal);
if (value) {
this.element.nativeElement.value = this._value;
} else {
this.element.nativeElement.value = '';
}
}
}
要使用此指令,您的html应该如下所示:
<mat-form-field>
<mat-label>Value</mat-label>
<input
type="text"
localizedNumericInput
matInput
autocomplete="off"
formControlName="value"
/>
</mat-form-field>