I would like to to style a select element in an Angular component differently, when a specific option (with an inactive attribute) is selected.
My component look like this:
class ComponentWithSelect {
@Input() ngModel: unknown = null;
@Input() options: { value: unknown, label: string, diasbled: boolean }[]
inactiveSelected: boolean = false;
...
}
and the corresponding template look like this:
<select
[ngModel]="ngModel"
[class.inactive-select]="inactiveSelected">
<option *ngFor="let option of options"
[ngValue]="option.value"
[disabled]="option.disabled">
{{option.label}}
</option>
</select>
I would like to apply the inactive-select class when an option with the inactive attribute is selected OR when a ngModel is passed to the component which corresponds to an option with an inactive attribute.
I tried to use the (ngModelChange)
to set the inactiveSelected property but $event
parameter passed to a corresponding method (onModelChange) does contain the value of the selected option, not the inactive attribute.
I could go with (change)
and access the event.target.options.selectedIndex property but neither of those two approaches works, when the ngModel is set from outside or its initial value is so, that it corresponds to a option with an inactive property
EDIT:
I tried the suggested approach with using the whole option object for ngValue
class ComponentWithSelect {
@Input()
set ngModel(model: unknown) {
this.selectedValue = this.options.find(option => option.value === model) || null;
}
@Input() options: { value: unknown, label: string, disabled: boolean }[] = [];
@Output() ngModelChange = new EventEmitter<unknown>();
selectedValue: { value: unknown, label: string, disabled: boolean } = null;
onModelChange(option: { value: unknown, label: string, disabled: boolean }) {
this.ngModel = option.value;
this.ngModelChange.emit(this.ngModel);
}
and the template
<select
[(ngModel)]="selectedValue"
[class.inactive-select]="selectedValue.disabled">
<option *ngFor="let option of options"
[ngValue]="option">
{{option.label}}
</option>
</select>
The right option is now selected properly when a corresponding ngModel is passed to my component, but when I try to manually change the select, for the for the first time nothing is selected, all consequent changes are working fine.