简体   繁体   English

Angular - ngOnInit()

[英]Angular - ngOnInit()

Is it possible to access both this.droneForm and this.service?是否可以同时访问 this.droneForm 和 this.service? At the moment the one on top is taking priority目前最上面的那个是优先的

ngOnInit(){
    this.droneForm = this.formBuilder.group({
      serialNumber:['', [Validators.required]],
      modelNumber:['', [Validators.required]],
      brand:['', [Validators.required]],
      model:['', [Validators.required]],
      ownerIDNumber:['', [Validators.required,Validators.pattern("[0-9]+[A-Za-z]")]],
      ownerName:['', [Validators.required]],
      ownerSurname:['', [Validators.required]],
      ownerContactNumberCountryCode:['', [Validators.required]],
      ownerContactNumber:['', [Validators.required,Validators.minLength(8)]],
      ownerEmail:['', [Validators.required,Validators.pattern("^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$")]]
    }
    );
    this.service.getDronebyId(this.droneid)?.subscribe(detailedDrone => {
      this.drone = detailedDrone;
  })
}

Assuming that what you need is to place the values of the detailedDrone into the form, all you need is to call patchValue on the form once the services responds:假设您需要将detailedDrone的值放入表单中,您只需在服务响应后在表单上调用patchValue

(...)

this.service.getDronebyId(this.droneid)?.subscribe(detailedDrone => {
  this.drone = detailedDrone;
  this.droneForm.patchValue(detailedDrone)
})

for that to work, the droneForm controls must have the same names as the properties of detailedDrone为此, droneForm控件必须与detailedDrone的属性具有相同的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM