You currently have a <select>
, and you want to listen for changes in its selected value. You are attempting to do this by calling a function in your component in the onChange
handler.
<select onChange="updateSelectedSystem(availableSystem(availableSystem))">
<!-- options -->
</select>
This is not how you handle form control changes in Angular. You could use [(ngModel)]
to bind to a property, and use (onModelChange)
to listen to value changes. This is a simple approach for simple scenarios.
I would recommend using forms, as forms are more flexible when you want to add complexity, and are still simple to set up.
To do this, build your form in your component:
constructor(private formBuilder: FormBuilder) {}
form: FormGroup;
selected: number[];
ngOnInit() {
this.form = this.formBuilder.group({
systems: this.formBuilder.control([])
});
}
onChange() {
this.selected = this.form.value.systems;
}
And bind to your HTML using the forms directives:
<form [formGroup]="form" (change)="onChange()">
<select multiple formControlName="systems">
<!-- options -->
</select>
</form>
As your form grows in complexity, the form directives mirror the structure of the form group you build in your component.
The simple version
You can always just bind to the change
event on the select directly:
<select (change)="onChange()">
<!-- options -->
</select>
DEMO: https://stackblitz.com/edit/angular-k3k2nn