Angular 8 | 如何从一个组件填充表单到另一个组件?

huangapple go评论54阅读模式
英文:

Angular 8 | How to fill a form from one component to another?

问题

I'm trying to populate this form from one component to another.

我尝试从一个组件填充另一个组件的表单。

I created a object of inserimento.component.ts inside the home.component.ts. Any function I call of InserimentoComponent inside HomeComponent works. For example the console.log works fine, but when I try to set the form values they don't graphically appear.

我在home.component.ts内创建了一个inserimento.component.ts的对象。我调用InserimentoComponent内的任何函数都有效。例如,console.log正常工作,但当我尝试设置表单值时,它们不会在界面上显示。

From debug, however, the values are passed correctly. Also, if instead of

但是,从调试中可以看出,值被正确传递。另外,如果不使用

this.formInserimento.controls['matricola'].setValue(this.inserimento.matricola);

I use

我使用

(document.getElementById('matricola') as HTMLInputElement).value = this.inserimento.matricola

it works properly.

它可以正常工作。

However, with the setValue I can't get it to work. What am I doing wrong?

然而,使用setValue时无法使其工作。我做错了什么?

Can anyone give me some tips? I've been looking for a solution for hours.

有人能给我一些建议吗?我已经寻找解决方案几个小时了。

Thanks to whoever will answer!

感谢回答的人!

My home.component.ts

export class HomeComponent implements OnInit {
  public inserimentoComponent: InserimentoComponent;
  
  constructor(private channelService: ChannelService,
    private flowService: FlowService,
    private router: Router,
    private wizardService: WizardService, public fb: FormBuilder) {
  }

  ngOnInit() {
    this.inserimentoComponent = new InserimentoComponent(this.wizardService, this.flowService, this.fb);

    this.inserimentoComponent.formInserimento = this.fb.group({
      matricola: new FormControl('', Validators.required),
      name: new FormControl('', Validators.required),
    })
  }

  goToInserimento(id: any): void {
    this.router.navigate(['/wizard']);

    const delay = new Promise(resolve => setTimeout(resolve, 500));
    delay.then(() => {
      const linkElement = document.querySelector('a[routerLink="inserimento"]') as HTMLElement;
      if (linkElement) {
        linkElement.click();
      }
    });
    delay.then(() => {
      this.inserimentoComponent.getInserimentoById(id)
    });
  }
}

My inserimento.component.ts

export class InserimentoComponent implements OnInit {

  // Initialize the form
  public formInserimento: FormGroup;

  constructor
    (private wizardService: WizardService,
      private flowService: FlowService,
      public fb: FormBuilder) {
    this.formInserimento = this.fb.group({
      matricola: new FormControl('', Validators.required),
      name: new FormControl('', Validators.required),
    })
  }

  getInserimentoById(id) {
    console.log(id) // it works
    this.wizardService.getInserimentoById(id).subscribe(response => {
      this.inserimento = response.result; // it works
      console.log(response.result)
      if (response.result != null) { // it works
        this.formInserimento.controls['matricola'].setValue(this.inserimento.matricola); // it doesn't work
        this.formInserimento.controls['name'].setValue(this.inserimento.name); // it doesn't work
      }
    });
  }
}
英文:

I'm trying to populate this form from one component to another.

I created a object of inserimento.component.ts inside the home.component.ts. Any function I call of InserimentoComponent inside HomeComponent works. For example the console.log works fine, but when I try to set the form values they don't graphically appear.

From debug, however, the values are passed correctly. Also, if instead of

this.formInserimento.controls['matricola'].setValue(this.inserimento.matricola);

I use

(document.getElementById('matricola') as HTMLInputElement).value = this.inserimento.matricola

it works properly.

However, with the setValue I can't get it to work. What am I doing wrong?

Can anyone give me some tips? I've been looking for a solution for hours.
Thanks to whoever will answer!

My home.component.ts

export class HomeComponent implements OnInit {
  public inserimentoComponent: InserimentoComponent;
  
  constructor(private channelService: ChannelService,
    private flowService: FlowService,
    private router: Router,
    private wizardService: WizardService, public fb: FormBuilder) {
  }

  ngOnInit() {
    this.inserimentoComponent = new InserimentoComponent(this.wizardService, this.flowService, this.fb);

    this.inserimentoComponent.formInserimento = this.fb.group({
      matricola: new FormControl('', Validators.required),
      name: new FormControl('', Validators.required),
    })
 
  }


  goToInserimento(id: any): void {
    this.router.navigate(['/wizard']);

    const delay = new Promise(resolve => setTimeout(resolve, 500));
    delay.then(() => {
      const linkElement = document.querySelector('a[routerLink="inserimento"]') as HTMLElement;
      if (linkElement) {
        linkElement.click();
      }
    });
    delay.then(() => {
      this.inserimentoComponent.getInserimentoById(id)
    });
  }

}

My inserimento.component.ts

export class InserimentoComponent implements OnInit {

  //Inizializza il form
  public formInserimento: FormGroup;

  constructor
    (private wizardService: WizardService,
      private flowService: FlowService,
      public fb: FormBuilder) {
    this.formInserimento = this.fb.group({
      matricola: new FormControl('', Validators.required),
      name: new FormControl('', Validators.required),

    })

  }


  getInserimentoById(id) {
    console.log(id) //it works
    this.wizardService.getInserimentoById(id).subscribe(response => {
      this.inserimento = response.result; //it works
      console.log(response.result)
      if (response.result != null) { //it works

        this.formInserimento.controls['matricola'].setValue(this.inserimento.matricola); //it doesn't work
        this.formInserimento.controls['name'].setValue(this.inserimento.name); //it doesn't work

    });

  }

}

答案1

得分: 1

使用响应式表单,您可以使用.patchValue()来设置表单控件的值。

this.formInserimento.patchValue({
  matricola: this.inserimento.matricola,
  name: this.inserimento.name,
})

在设置值后,更新值和有效性:

this.formInserimento.controls['matricola'].updateValueAndValidity();
this.formInserimento.controls['name'].updateValueAndValidity();

或者,您也可以使用以下方法:

this.formInserimento.controls['matricola'].updateValue(this.inserimento.matricola);
this.formInserimento.controls['name'].updateValue(this.inserimento.name);
英文:

With Reactive Forms you can use .patchValue() for setting the value on form controls

this.formInserimento.patchValue({
  matricola: this.inserimento.matricola,
  name: this.inserimento.name,
})

Update value and validity after patching the values

this.formInserimento.controls['matricola'].updateValueAndValidity();
this.formInserimento.controls['name'].updateValueAndValidity();

Or, you can also use -

this.formInserimento.controls['matricola'].updateValue(this.inserimento.matricola);
this.formInserimento.controls['name'].updateValue(this.inserimento.name);

答案2

得分: 0

使用响应式表单时,您可以在响应式表单的控件上使用 .patchValue()

this.formInserimento.get('matricola').patchValue(this.inserimento.matricola);
this.formInserimento.get('name').patchValue(this.inserimento.name);
英文:

As you are using Reactive Forms, you can use .patchValue() on the reactive form's control:

this.formInserimento.get('matricola').patchValue(this.inserimento.matricola);
this.formInserimento.get('name').patchValue(this.inserimento.name);

huangapple
  • 本文由 发表于 2023年2月18日 01:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定