I have a angular form with a select control which receives it's values from an enum on the server.
<select name="" id="" [formControl]="form.get('myType')">
<option *ngFor="let type of types" [value]="+type['key']">
{{ type['value'] }} ({{ type['key']}})
</option>
</select>
I've created an example here with the response hard coded in the types variable : stackblitz
I'm now trying to populate the form using the form patchValue method (see setValues button and method).
setValues() {
this.form.patchValue({
myType: 0
});
}
In real life this response comes form the server.
The model from the server is as follows:
public class TestModel
{
public MyType Type { get; set; }
}
So far this works.
If I change the model on the server to include the Json StringEnumConverter attribute:
public class TestModel
{
[JsonConverter(typeof(StringEnumConverter))]
public MyType Type { get; set; }
}
The patchValue method is now like this (see setValues2 button and method):
setValues2() {
this.form.patchValue({
myType: 'Type1'
});
}
However my value is no longer loaded into the select control.
How can I get around this?
I started creating a directive but I don't know how to intercept the bindings to change the value to the enums key equivalent.
Please help