Angular 4 setting selected option in Dropdown

前端 未结 6 1045
渐次进展
渐次进展 2020-12-13 17:34

several questions about this and diffrent answeres. But none of them realy answeres the question. So again:

Setting default of a Dropdown select by value isnt workin

6条回答
  •  天命终不由人
    2020-12-13 18:05

    To preselect an option when the form is initialized, the value of the select element must be set to an element attribute of the array you are iterating over and setting the value of option to. Which is the key attribute in this case.

    From your example.

    
    

    You are iterating over 'options' to create the select options. So the value of select must be set to the key attribute of an item in options(the one you want to display on initialization). This will display the default of select as the option whose value matches the value you set for select.

    You can achieve this by setting the value of the select element in the onInit method like so.

    ngOnInit(): void{
        myForm : new FormGroup({
           ...
           question.key : new FormControl(null)
        })
        // Get desired initial value to display on 
                            
        
    提交评论

提交回复
热议问题