[英]Can't set default select value in ReactiveForms , Angular 7
I have some problem with changeDetection as Angular doesnt update my reactive control value into the dropdown view. 我的changeDetection有问题,因为Angular不会将我的反应性控制值更新到下拉视图中。
I have reproduced the full problem in a simple StackBlitz example . 我已经在一个简单的StackBlitz示例中重现了完整的问题。
So basically how can i force Angular to show me 'Argentina' as selected default option in dropdown ? 因此,基本上,我怎样才能强制Angular在下拉菜单中向我显示“阿根廷”作为默认选项?
Would be very grateful if anyone give me a hint how to solve this 如果有人给我提示如何解决这个问题,将不胜感激
It seems like the the select options list does not contain that particular data you are trying to select. 选择选项列表似乎不包含您要选择的特定数据。
In your case, { "id": 5, "text": "Argentina" }
should be part of this.definitions
too. 在您的情况下, { "id": 5, "text": "Argentina" }
应该是this.definitions
一部分。
You can either push that object to this.definitions
before declaring your reactive form, 您可以在声明您的反应形式之前将该对象推送到this.definitions
,
constructor(private fb: FormBuilder){
this.definitions.push(this.default);
this.rootFormGroup = this.fb.group({
control: this.default
});
Or, you explicity include that object on this.definitions when you are writing your code. 或者,在编写代码时,可以在this.definitions中明确包含该对象。
definitions = [
{
"id": 1,
"text": "Poland"
},
{
"id": 2,
"text": "UK"
},
{
"id": 3,
"text": "Germany"
},
{
"id": 4,
"text": "France"
},
{
"id": 5,
"text": "Argentina"
}
]
You have to include the { "id": 5, "text": "Argentina" }
object in the definitions array you are binding to the select tag. 您必须在绑定到select标记的定义数组中包含{ "id": 5, "text": "Argentina" }
对象。 Without adding the object you cant bind the value. 如果不添加对象,则无法绑定值。
definitions = [
{
"id": 5,
"text": "Argentina"
},
{
"id": 1,
"text": "Poland"
},
{
"id": 2,
"text": "UK"
},
{
"id": 3,
"text": "Germany"
},
{
"id": 4,
"text": "France"
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.