Reputation: 221
If there is no data in the table, I am getting undefined from service but I want to display default value in formControl in that case. if data is there then working fine...Please help me and suggest what I need to do.
Upvotes: 22
Views: 101016
Reputation: 68635
When you create the group and controls inside it, you can also initialize them.
fb
is the FormBuilder
fb.group({
yourControl: [0, Validators.required] // '0' is the default value
});
Upvotes: 29
Reputation: 789
Using FormControl constructor:
myFormGroup = new FormGroup({
mycontrol: new FormControl('myDefaultValue'),
});
doc source: Angular.io - Initializing Form Controls
Upvotes: 1
Reputation: 9687
You can try with default value in form control.
myForm: FormGroup;
ngOnInit() {
this.myForm = this.fb.group({
name: ["test", [Validators.required, Validators.maxLength(30)]],
address: this.fb.group({
pin: ["123456", Validators.required]
})
});
}
Upvotes: 5