ให้นำเข้าข้อมูลข้อความยอมรับ numeric เปอร์เซ็นต์เดียวที่สี่เหลี่ยม

0

คำถาม

ทำข้อความข้อมูลเดียวที่ยอมรับค่าเปอร์เซ็นต์ได้นิดหน่อยุ่งยากนี่คือหนทางที่จะทำอย่างนั้นโดยไม่ทำการลบ numeric ค่าและเพิ่ม'%'ตอนที่ทุกอย่างจะจบลง

รู้สึกอิสระที่จะแบ่งปันความคิดของคุณ!

angular html regex typescript
2021-11-23 18:18:38
1

คำตอบที่ดีที่สุด

0

เพื่อที่จะทำให้ข้อความยอมรับลอยตัวเลขและเพิ่ม"%"ในที่สุด:

  • ในแฟ้ม html:
<input id="id" type="text" formControlName="percentControl" (focusin)="start($event)"(focusout)="end($event)" />
  • เข้ามาts แฟ้ม:
  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();
  }
  • กำลังเจอกับการโอนถ่ายข้อมูลและจากที่กลับมา-สิ้นสุด:
  1. โพสข้อมูล:
 //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('%', '');

  1. ได้ข้อมูล:
 //Use Angular percent pipe
 local: string = "en-US";
 percentPipe:PercentPipe = new PercentPipe(this.local);
 myVariable = this.percentPipe.transform(dataFromBackEnd/100);

2021-11-23 18:18:38

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่