1- Create a new attribute in your dataset:
preferences: {name:string,isActive:boolean}[] = [{name:'A',isActive:false},{name:'B',isActive:false},{name:'C',isActive:false}];
2- Assign the [displayWith]
property to mat-autocomplete
for handling value manipulation with mat-option
<mat-autocomplete requireSelection #auto="matAutocomplete" [displayWith]="displayData()">
3- Implement a function to control data binding in mat autocomplete
displayData = () => (value: string): string =>{return !value ? this.preferredData : value}
preferredData represents the default value e.g.
this.preferredData = "D"
4- Utilize ngClass directive on mat-option
<mat-option [ngClass]="{'activeOption': pref.isActive}"
then
.activeOption {background-color: var(--selected-option-color)}
5- Apply the new style to options with existing data and include a checkbox within mat-option tag if needed.
6- Update the preferences
object's isActive
property value to true | false
for managing ngClass changes. Use
(optionSelected)='updateMethod($event)'
on
mat-autocomplete
to modify the
preferences
object property.