Angular2 select default first option

前端 未结 10 1658
半阙折子戏
半阙折子戏 2021-01-04 08:32

How do i select the first option in a select by default in angular 2 the following code don\'t seems to work.

 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]

提交回复
热议问题