[英]Angular patch value with numbers for multiselect in reactive forms
Hi i'm wondering why the patch value doesnt work with when using numbers.嗨,我想知道为什么在使用数字时补丁值不起作用。 I have a select which gets populated with a number for the value and i get a array of numbers from the backend which i use to set the value.我有一个选择,其中填充了一个数字作为值,我从后端获取了一个数字数组,我用它来设置值。 But this doesnt work.但这不起作用。 But when the array values are converted to string it works.但是当数组值转换为字符串时,它就起作用了。
<mat-select formControlName="location" placeholder="Event Type" >
<mat-option *ngFor='let loc of locations' value="{{loc.id}}">{{loc.name}}</mat-option>
</mat-select>
this is used to set the value这用于设置值
setParticipants(value) {
this.AddEventForm.patchValue({
participants: value
});
}
the value that gets passed to set participants is [84,118] is something like this传递给集合参与者的值是 [84,118] 是这样的
EDIT- Created a stackblitz https://stackblitz.com/edit/angular-dvb4y5编辑 - 创建了一个 stackblitz https://stackblitz.com/edit/angular-dvb4y5
The way of binding the property value
makes the difference.绑定属性value
的方式有所不同。
If you bind like below, it will convert the data type to string
.如果你像下面这样绑定,它会将数据类型转换为string
。
<option *ngFor='let val of list' value='{{val.id}}'>{{val.name}}</option>
If you bind as below, it will bind the actual data type.如果按如下方式绑定,它将绑定实际的数据类型。
<option *ngFor='let val of list' [value]='val.id'>{{val.name}}</option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.