【问题标题】:FormControlName not showing the previous values in angular 7 material modalFormControlName 未在 Angular 7 材料模态中显示以前的值
【发布时间】:2019-06-28 03:26:32
【问题描述】:

我正在使用 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>


【问题讨论】:

  • i assign the values before opening the edit Modal. However, they don't fill the text boxes in the modal?。你给什么赋值?
  • @randomSoul 我正在为其表单控件名称分配值。 this.editTaskId = localStorage.getItem('editTaskId') this.editTaskName = localStorage.getItem('editTaskName')
  • 你的意思是modal controls?在打开模式之前,它们将不可用。
  • 点击模式后-$('#editModal').click();,我做了console.log的值,我仍然得到表单控件的输出。
  • @SuchetaShrivastava 能否提供 StackBlitz 代码让我可以重现相同的问题?

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


【解决方案1】:

你最好把[(ngModel)]和你的对象一起使用,你的typescript代码用你构建editTaskForm的方式不够清晰,所以我建议把代码组织清楚,一步一步,你会明白的工作。

你好!

【讨论】:

  • 抱歉,用 formControlName 替换 [(ngModel)] 也不起作用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多