0
我使用Angular2和我創建了一個指令把口罩上的投入,具體如下:指令在負載信息
import {Directive, Attribute} from 'angular2/core';
import {NgModel} from 'angular2/common';
@Directive({
selector: '[real]',
host: {
'(blur)': 'onInputChange()'
}
})
export class RealDirective {
modelValue: string;
viewValue: string;
constructor(public model: NgModel) {
}
onInputChange() {
this.modelValue = this.model.value.replace(/[^0-9\,]/g,'');
this.viewValue = RealDirective.format(this.modelValue);
this.model.viewToModelUpdate(this.modelValue);
this.model.valueAccessor.writeValue(this.viewValue)
}
static format(valor) {
if (valor == "") {
valor = "0";
}
var valorFloat: number = parseFloat(valor.replace(",", "."));
var valorString: string = valorFloat.toFixed(2).toString().replace(".", ",");
var valorSemCasasDecimais = valorString.substring(0, valorString.length - 3);
var valorArray: string[] = valorSemCasasDecimais.split("");
var casa: number = 0;
var valorStringComPontosInvertida: string = "";
for (var i = valorArray.length - 1; i > -1; i--) {
valorStringComPontosInvertida = valorStringComPontosInvertida + valorArray[i];
casa = casa + 1;
if (casa == 3 && i > 0) {
valorStringComPontosInvertida = valorStringComPontosInvertida + ".";
casa = 0;
}
}
var valorStringComPontosInvertidaArray: string[] = valorStringComPontosInvertida.split("");
var valorStringComPontosCorreta: string = "";
for (var i = valorStringComPontosInvertidaArray.length - 1; i > -1; i--) {
valorStringComPontosCorreta = valorStringComPontosCorreta + valorStringComPontosInvertidaArray[i];
}
var valorStringPronta: string = valorStringComPontosCorreta + valorString.substring(valorString.length - 3, valorString.length);
return valorStringPronta;
}
}
它運行模糊事件。 但我想執行加載每個輸入的表單上的信息,有沒有人有任何想法如何做到這一點?
謝謝邁克爾,但我不太明白。我在ngOnInit中使用該指令?我如何獲得輸入的價值,因爲我做了一些測試,並且它被執行了很多次,並且NgOnInit上的this.model爲null。 –