TS FILE
import { Component, ViewChild } from '@angular/core';
/**
* @title Basic select
*/
@Component({
selector: 'select-overview-example',
templateUrl: 'select-overview-example.html',
styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample {
resData = {
"data": ["Pune", "Mumbai", "Pimpari", "Nagpur", "Hydrabad", "Assam", "Karnataka", "Bihar", "Maharashtra", "Delhi", "Srinagar", "Shimla", "Goa", "Rajasthan", "MP", "Aandhra-Pradesh"]
}
selectOne : string ='';
selectTwo='';
selectThree='';
selectFour='';
one='';
@ViewChild('select2') _select2: any
firstSelections: string = '';
// setFirstValues(form) {
// this.firstSelections = form.value.select1
// if (this._select2.value) {
// const secondSelectionsValues = this._select2.value.slice();
// for (var i = secondSelectionsValues.length - 1; i >= 0; i--) {
// if (this.firstSelections.includes(secondSelectionsValues[i])) {
// secondSelectionsValues.splice(i, 1)
// this._select2.writeValue(secondSelectionsValues);
// }
// }
// }
// }
onChanged(){
this.resData.data.forEach(ele =>{
if(ele == this.selectOne)
this.selectTwo !== this.selectOne;
})
}
}
HTML
<form #myForm="ngForm">
<div class="col-md-4">
<mat-form-field >
<mat-select [(ngModel)]="selectOne" name="selectOne" (selectionChange)="onChanged()">
<mat-option *ngFor="let time1 of resData.data" value="time1" >{{time1}}</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="col-md-4">
<mat-form-field >
<mat-select [(ngModel)]="selectTwo" name="selectTwo" (selectionChange)="onChanged()">
<mat-option *ngFor="let time2 of resData.data" value="time2" >{{time2}}</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="col-md-4">
<mat-form-field>
<mat-select [(ngModel)]="selectThree" name="selectThree">
<mat-option *ngFor="let time3 of resData.data" value="time3" >{{time3}}</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="col-md-4">
<mat-form-field>
<mat-select [(ngModel)]="selectFour" name="selectFour">
<mat-option *ngFor="let time4 of resData.data" value="time4" >{{time4}}</mat-option>
</mat-select>
</mat-form-field>
</div>
<form>
Explanation: when I select a value in the first dropdown, that value is disabled in dropdowns two, three, and four. Each dropdown must have a unique value, ensuring no duplicate values. Should I use the (selectionchange) or (ngModelChange) events for this functionality? Thank you in advance.
Check out my stackblitz example here - https://stackblitz.com/edit/on-change-selection?file=app%2Fselect-overview-example.html