选择中的表单值



我有一个有棱角的页面,像:

catClient: any = [
[1, 'VALUE1', 'DescriptionValue1', true],
[2, 'VALUE2', 'DescriptionValue2', true],
];
selectedCategory: string;
customer = {
baseTypeIdentifier: '', // if isNewCustomer = false, baseTypeIdentifier will be for example: VALUE1 for BE
};
// customer = {
//  baseTypeIdentifier: 'VALUE1',
//  };
isNewCustomer: boolean = true;
selectCustomerType(newVal) {
console.log('newVal ', newVal);
console.log('catCliente ', this.catClient);
for (let i = 0; i < this.catClient.length; i++) {
if (newVal == this.catClient[i][0]) {
this.selectedCategory = this.catClient[i][2];
console.log('selectedCategory ', this.selectedCategory);
this.customer.baseTypeIdentifier = this.selectedCategory;
}
}
}
}

html:和

<div>
<select
*ngIf="isNewCustomer"
class="form-control"
[ngModelOptions]="{ standalone: true }"
[(ngModel)]="customer.baseTypeIdentifier"
(ngModelChange)="selectCustomerType($event)"
appTab
placeholder="SelectValue "
tabIndex="1"
[disabled]="!isNewCustomer"
>
<option value="-1" disabled="">Select Value</option>
<option
*ngFor="let cat of catClient"
name="fieldName"
ngDefaultControl
[value]="cat[0] | number"
>
{{ cat[1] }}
</option>
</select>
</div>

https://stackblitz.com/edit/angular-ivy-u5egtt?file=src/app/app.component.html

我在这里有一个问题,如果isNewCustomer = true我必须从选择中选择值(但我必须选择2时间出现),而如果isNewCustomer = false我应该显示客户的值。baseTypeIdentifier,但不工作

我该怎么办?

你的问题是,看看那行以@@@开头的注释。

我认为通过文本而不是通过值来分配它,这应该可以解决你的问题

selectCustomerType(newVal) {
console.log('newVal ', newVal);
console.log('catCliente ', this.catClient);
for (let i = 0; i < this.catClient.length; i++) {
if (newVal == this.catClient[i][0]) {
this.selectedCategory = this.catClient[i][2];
console.log('selectedCategory ', this.selectedCategory);
this.customer.baseTypeIdentifier = newVal;
}
}
}

测试在这里

最新更新