无法读取未定义的属性“ setValue”角度的

时间:2020-03-16 14:41:30

标签: html angular typescript

我的HTML组件出现问题,由于某种原因,我的方法显示错误: 当我单击“编辑”按钮时,无法读取未定义的属性“ setValue” ,但是一切正常。

这是我的HTML

<div *ngFor="let item of currentUser.myExperiences" class="mt-3">
        <div>
          <h6>{{item.companyName}} <span *ngIf="item.currentlyWorking" class="badge badge-warning">Currently
              working</span>
              <span class="float-right">
                <a class="text-warning cursor" (click)="openEditModal(item)"><i class="fas fa-pencil-alt"></i></a>
              </span>
            </h6>
          <p class="text-muted p-text-sm">{{item.functions}}</p>
          <p class="text-muted p-text-sm" *ngIf="item.references">
            <strong>References:</strong>
            {{item.references}}
          </p>
          <hr>
        </div>
      </div>

<form role="form" [formGroup]="updateForm">
        <div class="form-group">
          <label for="functions">Funtions</label>
          <input type="text" class="form-control" id="functions" name="functions" formControlName="functions" aria-describedby="emailHelp">
        </div>
        <div class="form-group">
          <label for="exampleInputPassword1">Password</label>
          <input type="password" class="form-control" id="exampleInputPassword1">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>

这是我的TS组件:

public openEditModal(myExperience: IMyExperience): void {
console.log(myExperience);
// Set values to controls
console.log(myExperience.id);
this.experienceId.setValue(myExperience.id);
this.companyName.setValue(myExperience.companyName);
this.functions.setValue(myExperience.functions);
this.workedYears.setValue(myExperience.workedYears);
this.references.setValue(myExperience.references);
this.currentlyWorking.setValue(myExperience.currentlyWorking);

this.updateForm.setValue({
  experienceId: this.experienceId.value,
  companyName: this.companyName.value,
  functions: this.functions.value,
  workedYears: this.workedYears.value,
  references: this.references.value,
  currentlyWorking: this.currentlyWorking.value
});

// Open the modal
this.modalRef = this.modalService.show(
  this.editModal,
  Object.assign({}, { class: 'gray modal-lg' })
);}

我尝试在控制台中显示该对象,但是它可以正常工作,但是我不确定为什么会这样。

我使用解析器将信息正确地从路由传递到组件。

先谢谢了。

1 个答案:

答案 0 :(得分:0)

尝试使用 this.updateForm.patchValue 而不是 this.updateForm.setValue

相关问题