Angular 角度值不适用于JsonConverter

Angular 角度值不适用于JsonConverter,angular,angular-reactive-forms,angular-directive,jsonconvert,jsonconverter,Angular,Angular Reactive Forms,Angular Directive,Jsonconvert,Jsonconverter,我有一个带有select控件的角度窗体,它从服务器上的枚举接收它的值 <select name="" id="" [formControl]="form.get('myType')"> <option *ngFor="let type of types" [value]="+type['key']"> {{ type['value'] }} ({{ type['key']}}) </option> </select> 在现实生

我有一个带有select控件的角度窗体,它从服务器上的枚举接收它的值

<select name="" id="" [formControl]="form.get('myType')">
  <option *ngFor="let type of types"  [value]="+type['key']">
    {{ type['value'] }} ({{  type['key']}})
  </option>
</select>
在现实生活中,此响应来自服务器

服务器的模型如下所示:

public class TestModel
{
    public MyType Type { get; set; }
}
到目前为止,这是可行的

如果我更改服务器上的模型以包含Json StringEnumConverter属性:

public class TestModel
{
    [JsonConverter(typeof(StringEnumConverter))]
    public MyType Type { get; set; }
}
patchValue方法现在是这样的(请参见setValues2按钮和方法):

但是,我的值不再加载到选择控件中

我怎样才能避开这件事

我开始创建一个指令,但我不知道如何拦截绑定以将值更改为等效的枚举键


请帮助

您已从类型数组中将选项值设置为属性

<option *ngFor="let type of types"  [value]="+type['key']">

由于您选择的项目基于
,因此没有值为
类型1
。因此您的
[value]
为空。您可以通过
value
找到它的键,并设置
[value]

setValues2() {
   this.form.patchValue({
      myType: this.getKey('Type1')
   });
}

getKey(typeName:string) {
    let key = this.types.find(f => f.value === typeName).key;
    return key ? key : 0;
}

我希望创建一个指令,因为一些端点模型有Json字符串转换器和一些DONTP可能我不知道您需要指令的情况,但只要您将选项的值指定为整个对象,这个对象的属性是什么并不重要,这是我想要的,但我想要一个指令来截取值的设置。我之所以想要一个指令,是因为我有多个具有类似代码的表单,patchValue调用在基类中,因此每个表单的类型都不同。
<option *ngFor="let type of types"  [value]="+type['key']">
<option *ngFor="let type of types"  [value]="type">
setValues() {
    this.form.patchValue({
      myType: this.types[0]
    });
  }

  setValues2() {
    this.form.patchValue({
      myType: this.types[1]
    });
  }
setValues2() {
   this.form.patchValue({
      myType: this.getKey('Type1')
   });
}

getKey(typeName:string) {
    let key = this.types.find(f => f.value === typeName).key;
    return key ? key : 0;
}