mdziob mdziob - 1 year ago 194
Javascript Question

Angular2 reactive form default input value

I've got following code that initialize form in my component:

let editMode: boolean = !!this.template.templateId;

this.editTemplateForm ={
templateId: {value: editMode ? this.template.templateId : '', validators: [Validators.required, this.templateIdValidator]},
dataStorageTime: {value: editMode ? this.template.dataStorageTime : this.globalParameter.dataStorageTime, Validators.required, Validators.pattern("[0-9]+")]});

Value of variable
is undefined, so
is false. For
correct default value is set in linked input in form. Hovewer, for templateId input is set to "[object Object]" instead of empty string.

I'm using angular2 final realease.

Answer Source

This is the solution to my problem:

this.editTemplateForm ={
templateId: [editMode ? this.template.templateId : '', [Validators.required, this.templateIdValidator]],
dataStorageTime: [editMode ? this.template.dataStorageTime : this.globalParameter.dataStorageTime, [Validators.required, Validators.pattern("[0-9]+")]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download