简体   繁体   English

Angular - 将 FormBuilder 值传递给数组导致空字符串值

[英]Angular - Passing FormBuilder values to Array results in empty string values

I have a form that I fill out using Angular's FormBuilder .我有一个使用 Angular 的FormBuilder填写的表单。 This is the FormBuilder representation in the component typescript file:这是组件 typescript 文件中的FormBuilder表示:

constructor(private Form: FormBuilder) { }

AddStatusBoxForm = this.Form.group({
    cpuTempTopic: [''],
    uptimeTopic: [''],
    timeTopic: [''],
  });

I want to create a data structure that I can put these form values into, like so:我想创建一个数据结构,我可以将这些表单值放入其中,如下所示:

array: any[] = [this.AddStatusBoxForm.get('cpuTempTopic')?.value, 
                  this.AddStatusBoxForm.get('uptimeTopic')?.value, 
                  this.AddStatusBoxForm.get('timeTopic')?.value];

However console logging the array, results in empty strings.但是,控制台记录数组会导致空字符串。 Yet if I console log the FormBuilder values on their own they print out fine:然而,如果我控制台自己记录FormBuilder值,它们会打印出来:

console.log(this.AddStatusBoxForm.get('cpuTempTopic')?.value) // prints out fine
console.log(this.AddStatusBoxForm.get('uptimeTopic')?.value)
console.log(this.AddStatusBoxForm.get('timeTopic')?.value)
console.log(this.array) // prints out empty string 

在此处输入图像描述

I can't figure out why this is the case, I can't seem to pass the FormBuilder values to any sort of data structure.我不明白为什么会这样,我似乎无法将FormBuilder值传递给任何类型的数据结构。 I've also tried a Map and the same occurs where the passed in values seem to end up as empty strings in the data structure.我还尝试了Map ,同样的情况发生在传入的值似乎以数据结构中的空字符串结尾的地方。 Yet accessing the FormBuilder values on their own seems to work fine.然而,单独访问FormBuilder值似乎工作正常。

I've even tried to console log the type of the values from the FormBuilder which results in string:我什至尝试通过控制台记录来自FormBuilder的值的类型,结果为字符串:

console.log(typeof(this.AddStatusBoxForm.get('cpuTempTopic')?.value))

在此处输入图像描述

Any help with this would be appreciated.对此有任何帮助,我们将不胜感激。

This is because initially all the properties of the FormBuilder are initialized with empty string so while storing their values into an array will result with empty string values.这是因为最初FormBuilder的所有属性都是用空字符串初始化的,因此将它们的值存储到数组中时将得到空字符串值。 You need to subscribe to value changes of the FormGroup in order to update your array values in real time.您需要订阅FormGroup的值更改,以便实时更新您的数组值。 You can refer the example below and bring modifications to your code accordingly.您可以参考下面的示例并相应地修改您的代码。

addStatusBoxFormChangesSubscription: Subscription;

ngOnInit(): void {
this.addStatusBoxFormChangesSubscription = merge(
      this.AddStatusBoxForm.get('cpuTempTopic').valueChanges,
      this.AddStatusBoxForm.get('uptimeTopic').valueChanges,
      this.AddStatusBoxForm.get('timeTopic').valueChanges
    ).subscribe(() => (this.array = [this.AddStatusBoxForm.get('cpuTempTopic')?.value, 
                  this.AddStatusBoxForm.get('uptimeTopic')?.value, 
                  this.AddStatusBoxForm.get('timeTopic')?.value];));
}

ngOnDestroy(): void {
    this.addStatusBoxFormChangesSubscription?.unsubscribe();
  }

It is always a good practice to unsubscribe from the subscription when not working on the same page anymore.当不再在同一页面上工作时,取消订阅始终是一个好习惯。 You can explore more about form group here FormGroup documentation .您可以在此处探索有关表单组的更多信息FormGroup 文档

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

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