这是我的表格:
chartform = this.formbuilder({
id:[],
typeOfchart : [],
indicators:[]
})
HTML:
<form [formGroup]="chartform">
<input formControlName = "id" >put an id </input>
<input formControlName = "typeOfchart" >type of the chart </input>
<select formControlName = "indicators">
<option *ngFor="let indicator of metricindicators" value="indicator">{{indicator}}</option>
</select>
</form>
我想在一个数组中恢复"indicators“的值,所以在component.ts中尝试了一下:
let indexes = chartform(['indicators']).value
console.log("indexes",indexes.length) // when I input for example "performance" in the field
"indicators" the console display "indexes 11" while it must display "indexes 1" it show me the
number of caracters not the number of elements in the array
我尝试了另一种方法:
let indexes : any = []
indexes.push(chartform(['indicators']).value)
console.log("indexes",indexes.length) // here it gives me "indexes 1" when I input"performance" but
when I put "performance,currency" in the field "indicators" the
console display "indexes 1" while it must display "indexes 2"
有谁能帮我解决这个问题吗?
发布于 2020-07-16 06:33:22
你的“指示器”它是一个唯一的值,而不是一个数组。要获取所需的值,请使用
this.chartform.value.indicators
//or
this.chartform.get('indicators').value
注意,您还可以使用ngModel来更改formControl目录(*)的值
<select [ngModel]="chartform.get('indicators').value?chartform.get('indicators').value[0]:null"
(ngModelChange)="chartform.get('indicators').setValue([$event])"
[ngModelOptions]="{standalone:true}">
<option *ngFor="let indicator of metricindicators" [value]="indicator">
{{indicator}}
</option>
</select>
NOTE2:注意这是[value]="indicator"
,你忘记了[
]
(*)可以,您可以在ReactiveForm中使用ngModel。此输入始终在“独立”中使用,也就是说,它独立于formGroup
https://stackoverflow.com/questions/62924813
复制