ฉันต้องการmarkAsDirtyการควบคุมทั้งหมดในไฟล์FormGroup.
ฉันต้องการmarkAsDirtyการควบคุมทั้งหมดในไฟล์FormGroup.
คำตอบ:
พบว่าObject.keysรับมือได้ ..
    Object.keys(this.form.controls).forEach(key => {
      this.form.get(key).markAsDirty();
    });
สำหรับ Angular 8+ ให้ใช้สิ่งต่อไปนี้ (ตามคำตอบของ Michelangelo):
    Object.keys(this.form.controls).forEach(key => {
      this.form.controls[key].markAsDirty();
    });
เพื่อสิ่งที่คุ้มค่ามีอีกวิธีหนึ่งที่ทำได้โดยไม่ต้องใช้เวทย์มนตร์ Object.keys (... ) :
for (const field in this.form.controls) { // 'field' is a string
  const control = this.form.get(field); // 'control' is a FormControl  
}
คำตอบที่ยอมรับนั้นถูกต้องสำหรับโครงสร้างแบบฟอร์มเรียบ แต่ไม่ได้ตอบคำถามเดิมอย่างสมบูรณ์ หน้าเว็บอาจต้องใช้ FormGroups และ FormArrays ที่ซ้อนกันและเราต้องคำนึงถึงสิ่งนี้เพื่อสร้างโซลูชันที่มีประสิทธิภาพ
public markControlsDirty(group: FormGroup | FormArray): void {
    Object.keys(group.controls).forEach((key: string) => {
        const abstractControl = group.controls[key];
        if (abstractControl instanceof FormGroup || abstractControl instanceof FormArray) {
            this.markControlsDirty(abstractControl);
        } else {
            abstractControl.markAsDirty();
        }
    });
}
instanceofงานได้เสมอหลังจากถูกถ่ายทอดโดย typescript?
                    instanceofOutstanding ไม่ใช่คีย์เวิร์ดเฉพาะ TypeScript ( developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ) ไม่ใช่classประเภทข้อมูล
                    การใช้คำตอบ @Marcos ฉันสร้างฟังก์ชันที่สามารถเรียกได้ว่าส่ง formGroup เป็นพารามิเตอร์และทำเครื่องหมายว่าเด็ก ๆ ของ formGroup ทุกคนควบคุมว่าสกปรกเพียงเพื่อให้สามารถใช้งานได้จากที่อื่น ๆ รอบ ๆ โค้ดที่ใส่ไว้ในบริการตัวอย่างเช่น
public touchAllFormFields(formGroup: FormGroup): void {
    Object.keys(formGroup.controls).forEach((key) => {
        formGroup.get(key).markAsDirty();
    });
}
หวังว่ามันจะช่วยได้;)
ดูเหมือนว่าget  ฟังก์ชันจะไม่ทำงานอีกต่อไปสำหรับการดึงค่าเฉพาะในรูปแบบของคุณใน Angular 8 ดังนั้นนี่คือวิธีที่ฉันแก้ไขตามคำตอบของ @Liviu Ilea
for (const field in this.myForm.controls) { // 'field' is a string
  console.log(this.myForm.controls[field].value);
}
    Object.keys( this.registerForm.controls).forEach(key => {
       this.registerForm.controls[key].markAsDirty();
    });นี่คือสิ่งที่ใช้ได้ผลสำหรับฉัน
private markFormGroupTouched(formGroup: FormGroup) {
  Object.keys(formGroup.controls).forEach((key) => {
    const control = formGroup.controls[key];
    control.markAsDirty();
    if ((control instanceof FormGroup)) {
      this.markFormGroupTouched(control);
    }
  });
}
ฉันสร้างฟังก์ชั่นนี้ขึ้นมา * ฉันมีการควบคุมที่มีชื่อ 'order' และส่งดัชนีให้เขา
{"conditionGroups": [
   {
     "order": null,
     "conditions": []
   }
  ]
}
updateFormData() {
    const control = <FormArray>this.form.controls['conditionGroups'];  
    control.value.map((x,index)=>{
    x.order = index; 
 })
Cannot invoke an expression whose type lacks a call signature. Type 'AbstractControl' has no compatible call signatures.ไม่มีใครรู้สาเหตุ