mdziob mdziob - 2 months ago 32
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.


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]+")]]