我有以下问题:我有一个包含 2 个项目的下拉列表。我需要默认情况下出现第一个,并且在选择两个值中的任何一个时,将显示在控制台中并保存在变量中。我有以下代码:
HTML
<td>
<mat-select name="tipoCdp" (change)="onChangeCdp($event.value)" [(ngModel)]="tipoCdpValue">
<ng-container *ngFor="let gas of tipoc">
<mat-option [value]="gas.viewValue">
{{gas.viewValue}}
</mat-option>
</ng-container>
</mat-select>
</td>
TS
interface tipoCdp {
value: string;
viewValue: string;
}
@Component({
selector: '...',
templateUrl: '...',
styleUrls: ['...']
})
export class showValue implements OnInit {
constructor (...) { ... }
tipoc: tipoCdp[] = [
{value: 'gasto-0', viewValue: 'GASTO'},
{value: 'modificacion-1', viewValue: 'MODIFICACIÓN ANEXO DECRETO'}
];
//selected = this.tipoc[0].value; --> /* With this line I was selecting the firts element but the value was burned */
onChangeCdp(event) {
console.log(event);
this.tipoc = event;
}
}
当我在“事件”中运行程序时,会出现所选项目,但随后控制台上会出现以下错误:
“尝试区分 'GASTO' 时出错。只允许使用数组和可迭代对象”
谢谢你的帮助!