FormControlName不显示角度7材质模态中的先前值

时间:2019-06-28 03:26:32

标签: javascript angular forms angular-material angular-reactive-forms

我正在使用mbd材质设计angular7模板。我有一组任务,每个任务都有一个编辑按钮。当我单击“编辑”按钮时,将打开一个模态。我希望此模式具有该字段的现有值。我可以通过formControlName完成。但这没有发生。我单击编辑按钮,然后单击该单击功能,然后在打开编辑模态之前分配值。但是,它们不会填充模式中的文本框。在控制台中,它出现在我单击模式按钮之前。请帮助

app.component.ts



  openEditModal(taskIdValue:number){

        console.log("Task Id Value = ", taskIdValue)
        for(var i = 0 ; i<this.result.length ; i++){
       if(this.result[i].taskId == taskIdValue ){
        console.log("this.result[i] = ", this.result[i])
        localStorage.setItem('editTaskId',this.result[i].taskId);

        localStorage.setItem('editTaskName',this.result[i]. taskName);


       }
      }

       this.editTaskId = localStorage.getItem('editTaskId')
      this.editTaskName = localStorage.getItem('editTaskName')

     $('#editModal').click();

     this.displayEdit = true;
       }

       editTask(){
    console.log("this.result.taskId", )


      }

app.component.html

             <td>
                <button class = "btn btn-primary waves-light" 
       (click)="openEditModal(data.taskId)">Edit</button>
                <button type="button" id = "editModal" style = 
         "visibility:hidden" class="btn btn-primary waves-light" data- 
           toggle="modal" data-target="#EditExample"
                  (click)="centralLarge.show()" mdbWavesEffect>
                  Edit
                </button>
                <div mdbModal #centralLarge="mdb-modal" class="modal fade" 
           id="EditExample" tabindex="-1" role="dialog"
                  aria-labelledby="myModalLabel" aria-hidden="true" 
           [config]="{backdrop: true, ignoreBackdropClick: false}">
                  <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h4 class="modal-title w-100" id="myModalLabel">Edit 
           Data</h4>
                        <button type="button" class="close" data- 
           dismiss="modal" aria-label="Close" (click)="centralLarge.hide()">
                          <span aria-hidden="true">×</span>
                        </button>
                      </div>
                      <div class="modal-body" *ngIf = "displayEdit">
                        <div>
                          <div>
                            <form (ngSubmit)="editTask(editTaskForm.value)" 
         [formGroup]="editTaskForm">

                              <div class="form-group">
                                <label>Task Id : </label>
                                <input type="text" maxlength="100" 
        class="form-control" placeholder="Enter Task Id "
                                  formControlName="editTaskId" 
           [formControl]="editTaskForm.controls['editTaskId']"
                                  required>

                              </div>
                              <div class="form-group">
                                <label>Task Name : </label>
                                <input type="text" maxlength="1000" 
            class="form-control" placeholder="Enter Task Name "
                                  formControlName='editTaskName' 
            [formControl]="editTaskForm.controls['editTaskName']"
                                  required>

                              </div>

                              <div class="modal-footer">
                                <button type="button" class="btn btn- 
         secondary btn-sm waves-light" data-dismiss="modal"
                                  (click)="centralLarge.hide()" 
       mdbWavesEffect>Close</button>
                                <button type="submit" 
         [disabled]="!editTaskForm.valid" class="btn btn-primary btn-sm 
         waves- 
            light"
                                  (click)="centralLarge.hide()" 
          mdbWavesEffect>Save </button>
                              </div>
                            </form>
                          </div>

                        </div>
                      </div>

                    </div>
                  </div>
                </div>
              </td>


2 个答案:

答案 0 :(得分:0)

您最好将[(ngModel)]与对象一起使用,您的打字稿代码在构建editTaskForm的方式上还不够清楚,所以我建议清晰组织代码,并逐步进行,您将使其正常工作。

格里兹!

答案 1 :(得分:0)

你可以使用this.formGroupname.patchValue({})来设置之前的值,例如 this.form.patchValue({ fullname1: this.fullname1, });