在我的TS中我有:
private accountTypes = [
{ accountTypeId: 1,
displayName: "Individual - Taxable",
isTaxable: 1,
subcategory: "Taxable" },
{ },
{ }
... more objs
]
我在HTML中有这个:
<div *ngFor="let question of section?.questions">
<select class="q-select"
[(ngModel)]="acttypetest" // BIND HERE
name="answerForQuestion{{ question?.questionId }}">
<optgroup label="{{subCat?.subCat}}"
*ngFor="let subCat of accountTypes; let i = index;">
<option *ngFor="let acctType of accountTypes"
[ngValue]="acctType">
{{ acctType?.displayName }}
</option>
</optgroup>
</select>
</div>
当我尝试将accountTypes
数组中的第一个元素绑定到acttypetest
时,它可以正常工作:
setSelectedAcctType() {
this.acttypetest = this.accountTypes[0];
}
但是当我尝试像这样绑定它时(使用完全匹配我accountTypes
数组中第一个元素的常规对象,它不起作用:
setSelectedAcctType() {
this.acttypetest = {
accountTypeId: 1,
displayName: "Individual - Taxable",
isTaxable: 1,
subcategory: "Taxable"
};
}
如果第二种方式与我accountTypes
数组中的第一个元素完全相同的格式化对象,为什么第二种方法不起作用?
答案 0 :(得分:0)
与私有变量的绑定我认为这就是问题所在。尽量不要让非私人。
accountTypes = [
{ accountTypeId: 1,
displayName: "Individual - Taxable",
isTaxable: 1,
subcategory: "Taxable" },
{ },
{ }
... more objs
]