首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在angular中获取form Array中的所有数据?

如何在angular中获取form Array中的所有数据?
EN

Stack Overflow用户
提问于 2021-10-28 13:06:56
回答 1查看 47关注 0票数 0

我有这个输入数组,我想把它作为一个数组来发送,就像这个例子中的array exemple,我们如何使用input img来解决这个问题。

我有这样的输入

代码语言:javascript
复制
            <div>
                    <mat-form-field class='social-input lg:social-input-lg mt-8'>
                        <input  matInput type='text' placeholder="Instagram URL">
                    </mat-form-field>

                <mat-form-field class='social-input lg:social-input-lg mt-8'>
                        <input  matInput type='text' placeholder="facebook URL">
                    </mat-form-field>
                <mat-form-field class='social-input lg:social-input-lg'>
                              <input matInput type='text' placeholder="Linkedin URL">
                </mat-form-field>

            </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-28 13:53:18

代码语言:javascript
复制
export class AppComponent {
  constructor(private fb: FormBuilder) {}

  socForm: FormGroup = this.fb.group({
    instagram: ["", []],
    facebook: ["", []],
    linkedin: ["", []]
  });

  ngOnInit() {
    this.socForm.valueChanges
      .pipe(
        map((formVal: { instagram: string; facebook: string; linkedin: string }) => {
          return Object.entries(formVal).reduce((accu, [fK, fV]) => {
            return [...accu, { type: fK, url: fV }];
          }, []);
        })
      )
      .subscribe(console.log);
  }
}

https://codesandbox.io/embed/focused-wilbur-f4hvm?fontsize=14&hidenavigation=1&module=%2Fsrc%2Fapp%2Fapp.component.ts&theme=light

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69754655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档