Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value in formControl in angular 5

Tags:

angular

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.

like image 854
Amit Shaw Avatar asked Jul 03 '18 10:07

Amit Shaw


Video Answer


2 Answers

When you create the group and controls inside it , you can also initialize them.

fb is the FormBuilder

fb.group({
   yourControl: [0, Vaidators.required] // '0' is the default value
});
like image 133
Suren Srapyan Avatar answered Sep 21 '22 13:09

Suren Srapyan


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]
        })
    });
}
like image 42
Krishna Rathore Avatar answered Sep 21 '22 13:09

Krishna Rathore