I have the following problem: I have a drop down list with 2 items. I need the first one to appear by default and that when selecting either of the two its value is shown in the console and saved in a variable. I have the following code:
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;
}
}
When I run the program in "event" the selected item appears, but then the following error appears on the console:
"Error trying to diff 'GASTO'. Only arrays and iterables are allowed"
Thanks for your help!