입력 할 때 입력을 USD 통화로 형식화하고 싶습니다. 입력은 소수점 2 자리를 가지며 오른쪽에서 왼쪽으로 입력됩니다. 54.60을 입력하면 $ 0.05 -> $ 0.54 -> $ 5.46 -> $ 54.60으로 입력된다고 가정하십시오. 이 PLUNKER은 정확하게 이것을 수행하지만 각도 j에 있습니다.각도 2의 통화 형식을 입력하십시오.
<input name="cost" placeholder="cost" class="form-control" type="text" currency formControlName="cost" (rawChange)="rawCurrency=$event">
업데이트 : 마지막으로 나를 위해 일한 것을가
입니다 :
onInputChange(event: any, backspace: any) {
var newVal = (parseInt(event.replace(/[^0-9]/g, ''))/100).toLocaleString('en-US', { minimumFractionDigits: 2 });
var rawValue = newVal;
if(backspace) {
newVal = newVal.substring(0, newVal.length - 1);
}
if(newVal.length == 0) {
newVal = '';
}
else {
newVal = newVal;
}
// set the new value
this.model.valueAccessor.writeValue(newVal);
this.rawChange.emit(rawValue)
}
import {Directive, Output, EventEmitter} from '@angular/core';
import {NgControl} from '@angular/forms';
@Directive({
selector: '[formControlName][currency]',
host: {
'(ngModelChange)': 'onInputChange($event)',
'(keydown.backspace)':'onInputChange($event.target.value, true)'
}
})
export class CurrencyMask {
constructor(public model: NgControl) {}
@Output() rawChange:EventEmitter<string> = new EventEmitter<string>();
onInputChange(event: any, backspace: any) {
// remove all mask characters (keep only numeric)
var newVal = event.replace(/\D/g, '');
var rawValue = newVal;
var str = (newVal=='0'?'0.0':newVal).split('.');
str[1] = str[1] || '0';
newVal= str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + '.' + (str[1].length==1?str[1]+'0':str[1]);
// set the new value
this.model.valueAccessor.writeValue(newVal);
this.rawChange.emit(rawValue)
}
}
및
가 HTML에서 같이 사용되는 :처럼 지금까지 나의 지시 본다
질문을 진술 해주세요. – Blauhirn
명시된 PLUNKER가 앵귤러 2에서 수행하는 작업을 수행하려는 경우 오른쪽에서 왼쪽으로 입력을 입력하고 서식을 즉시 지정하는 방법 – Jane