Angular - FormGroup - setValue 方法忽略特定的字符串值

问题描述

我最近遇到了一个很奇怪的问题。我正在尝试设置值以形成角度的控制器

this.form.controls['WhateverKeyIS'].setValue('ABC');

以上有效,但如果我设置不同的值

this.form.controls['WhateverKeyIS'].setValue('B10');

this.form.controls['WhateverKeyIS'].setValue('B05');

控制器完全忽略这些并且不设置控制值,我假设 setValue 自动将那些 B05 转换为十六进制代码并失败,我无法进行任何其他逻辑推论。我已经尝试了一切以确保我正在发送字符串,但在这些值上总是失败。

任何帮助将不胜感激!

我使用的是 angular 8。

编辑 -

@input() form: FormGroup;
  onSelectOption(item) {
      this.form.controls[this.question.typeVal[0].key].setValue(item.key);
    console.log('select:',this.form.controls);
  }

onSelect 在 ui 中被调用

    <app-bootstrap-dropdown
      [title]="question.typeVal[0].label"
      [placeholder]="'Search '+question.typeVal[0].label"
      [currentItem]="initialValues[question.typeVal[0].key]"
      [collection]="question.typeVal[0].values"
      (selectionEvent)="onSelectOption($event)"
      (removeSelection)="onRemoveSelection()"
      [displayFunc]="getDropdownValue"
    ></app-bootstrap-dropdown>

但即使我将 item.key 更改为字符串 'B05 或 B10',它仍然不起作用。

解决方法

m *如果我们像这样声明表单组: 登录表单 = 新表单组({ WhatKeyIS: new FormControl(null),});

IN the method where we need to set the value:

patchValues(){
    this.loginForm.controls['WhateverKeyIS'].setValue('B10');
    console.log(this.loginForm.value,"values")
}

The above console is printed with the value which is set.Let me know if you are doing in different way?*