How do i select the first option in a select by default in angular 2 the following code don\'t seems to work.
If you use Model Driven Forms you can set the selected default value of your in your component (ngOnInit())
componenet.file.ts
public fieldSelect : any;
ngOnInit(){
...
this.fieldSelect= { key: "mySelect", label: "Example Select"};
this.controlsConfig= [];
this.controlsConfig[this.fieldSelect.key] = [""];
this.myFormGroup= this.formBuilder.group(this.controlsConfig);
...
}
component.file.html
Model-Driven Forms: While using directives in our templates gives us the power of rapid prototyping without too much boilerplate, we are restricted in what we can do. Reactive forms on the other hand, lets us define our form through code and gives us much more flexibility and control over data validation.+
There is a little bit of magic in its simplicity at first, but after you're comfortable with the basics, learning its building blocks will allow you to handle more complex use cases.
[example]