How can I get the selected value from a select component?
select.component.ts:
export class PfSelectComponent implements OnInit {
constructor() { }
ngOnInit() {
}
@Input() options : Array<Object>;
}
select.component.html
<select [(ngModel)]="selectedValue" id="exampleFormControlSelect1" class="form-control">
<option *ngFor="let option of options" [ngValue]="option.value">{{option.name}}</option>
</select>
select value: {{selectedValue}}
{{selectValue}} does not show the value selected in the component.
select.component.html
You shoud use [value] not [ngValue] : [ngValue] => [value]
<select [(ngModel)]="selectedValue" id="exampleFormControlSelect1" class="form-control" >
<option *ngFor="let option of options" [value]="option.value">{{option.name}}</option>
</select>
select value: {{selectedValue}}
select.component.ts
and add public selectedValue;
export class PfSelectComponent implements OnInit {
public selectedValue;
constructor() { }
ngOnInit() {
}
@Input() options : Array<Object>;
}
I tested this with
options = [
{
value: 1,
name : "1"
},
{
value: 2,
name : "2"
},
{
value: 3,
name : "3"
}
]
It works well :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With