Select default option value from typescript angular 6

前端 未结 14 1649
轻奢々
轻奢々 2020-12-09 07:30

I have a select html like this:


       
   
   . . .

Logic file: yourelement.component.ts

In the initialization of FormBuilderobject, in ngOnInit() function, set the default value you desire to be as default selected.

. . .
// Remember to add imports of "FormsModule" and "ReactiveFormsModule" to app.module.ts
import { FormBuilder, FormGroup } from '@angular/forms';
. . .

export class YourElementComponent implements OnInit {
  // 
variable uploadForm: FormGroup; constructor( private formBuilder: FormBuilder ){} ngOnInit() { this.uploadForm = this.formBuilder.group({ . . . form_variable: ['0'], // <--- Here is the "value" ID of default selected . . . }); } }

提交回复
热议问题