기 텍스트 입력만 받아들이 퍼센트 값을 조금 힘들 수 있습니다 여기를 할 수 있는 방법을 삭제하여 비 숫자 값을 추가하는'%'니다.
를 공유하여 주시기 바랍니다.
기 텍스트 입력만 받아들이 퍼센트 값을 조금 힘들 수 있습니다 여기를 할 수 있는 방법을 삭제하여 비 숫자 값을 추가하는'%'니다.
를 공유하여 주시기 바랍니다.
하기 위해서는 텍스트 입력 받아들이 떠 번호 추가"%"끝:
<input id="id" type="text" formControlName="percentControl" (focusin)="start($event)"(focusout)="end($event)" />
end(e) {
// console.log(/^[0-9.]*$/.test(e.target.value));
if(!/^[0-9.]*$/.test(e.target.value))
e.target.value = e.target.value.replaceAll(/[^0-9.]/g, '').trim();
//add ' %' at the end
if(e.target.value.length)
e.target.value = e.target.value+ ' %';
//this part is needed when working with angular form validation (ngForm required
//or formGroup Validators.required), else null value won't trigger the validation
else
e.target.value = '0 %';
}
start(e) {
e.target.value = e.target.value.replace('%', '').trim();
}
//make sure to get rid from ' %' when posting data to the backend
//example with formControl
// the + is for converting string to number
dataToPost = +this.form.get('percentControl').value.replaceAll('%', '');
//Use Angular percent pipe
local: string = "en-US";
percentPipe:PercentPipe = new PercentPipe(this.local);
myVariable = this.percentPipe.transform(dataFromBackEnd/100);