无法获取表单中对象的属性

问题描述 投票:0回答:1
angular typescript forms lifecycle ngoninit
1个回答
0
投票

这是因为由于 getPersonById 方法的异步特性。当您订阅它时,订阅块内的回调函数会在数据可用时执行,这意味着表单控件会在订阅之前使用人员的数据进行初始化。数据已获取。

ngOnInit(){
  this.personService.getPersonById(this.guard.person_id).subscribe((person) => {
    this.person = person
    this.editPersonForm = this.fb.group({
       surname: [this.person.surname,Validators.required],
       name: [this.person.name,Validators.required],
       father_name: this.person.father_name,
       birthdate: this.person.birthdate,
       phone_num: this.person.phone_number,
    })
    
  });
© www.soinside.com 2019 - 2024. All rights reserved.