[英]Angular ngModel not select with ngFor [HTML SELECT INPUT]
https://stackblitz.com/edit/angular-j7iiaw?file=src/app/app.component.ts https://stackblitz.com/edit/angular-j7iiaw?file=src/app/app.component.ts
The select input is "" instead 1 选择输入为“”而不是1
HTML 的HTML
<select [(ngModel)]="subcategoria.categoria" class="input-default" name="status_categoria" id="status_categoria">
<option *ngFor="let categoria of categorias" value="{{categoria.id}}">{{categoria.nome}}</option>
</select>
Typescript 打字稿
subcategoria: Array<any> = [{
categoria:1
}];
categorias: Array<any> = [{id:1,nome:"one"},{id:2,nome:"two"}];
subcategoria
is an Array, I think you want to access subcategoria[0].categoria
. subcategoria
是一个数组,我想您想访问subcategoria[0].categoria
。 Or you need to change the value of subcategoria
to 或者您需要将
subcategoria
的值更改为
subcategoria = {
categoria:1
};
In this, [(ngModel)]="subcategoria.categoria"
在此,
[(ngModel)]="subcategoria.categoria"
subcategoria is an array and not an object
so use [(ngModel)]="subcategoria[0].categoria"
for the default value 因此,请使用
[(ngModel)]="subcategoria[0].categoria"
作为默认值
You are using array as ngModel
, so you have to change the ngModel
as below, 您将数组用作
ngModel
,因此必须如下更改ngModel
,
[(ngModel)]="subcategoria[0].categoria"
Since categoria
is the 0th
index of the subcategoria
array you have to change the ngModel
to bind the value 由于
categoria
是subcategoria
数组的0th
索引,因此您必须更改ngModel
来绑定值
subcategeria is an array so you should address it like this [(ngModel)]=subcategeria[0].categeria
Or another solution is to change the subcategeria Array to an Object: 或者另一个解决方案是将子类别数组更改为对象:
subcategoria = {
categeria:1
};
您需要将subcategoria
从数组更改为对象,或者使用arr[index]
访问对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.