【问题标题】:How do I transfer values from the store to the form?如何将值从商店传输到表单?
【发布时间】:2022-08-15 13:41:12
【问题描述】:

我以这种方式从商店获取数据:

  ngOnInit(): void {
    this.store.pipe(select(getPerson))
    this.store.dispatch(loadPerson());
  }

但我需要能够将数据从商店传输到表单。

下面是一个在没有ngrx的情况下如何执行请求的示例:

formPerson: FormGroup = this.fb.group({});

this.personService.fetch()
.pipe(
  tap((user) => {
    this.formPerson.get(\'username\')?.setValue(user[\'username\']);
    this.formPerson.get(\'full_name\')?.setValue(user[\'full_name\']);
    this.formPerson.get(\'email\')?.setValue(user[\'email\']);
    this.formPerson.get(\'phone\')?.setValue(user[\'phone\']);
  })
)

    标签: angular ngrx


    【解决方案1】:

    如果服务返回与角度反应形式相同的属性名称,那么您可以使用以下方法。

    this.store.pipe(select(getPerson),filter(obj=>obj!=null))
          .subscribe((person) => {
            this.formPerson.patchValue(person);
          })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2018-04-05
      • 2020-12-13
      • 2013-01-08
      • 2021-10-22
      • 2018-09-11
      • 1970-01-01
      相关资源
      最近更新 更多