Reputation: 4749
I want to get the previous selected value in primeng dropdown .
<p-dropdown [options]="options" [filter]="true" [(ngModel)]="selectedType" (onChange)="onSelectType($event)" formControlName="alertType"></p-dropdown>
On change event I want to check whether the selected type is valid using an api, but need to revert if it is invalid. Since I am using ngModel, the value of selectedType is changing whenever the dropdown value changes.
So how can I get the previous selected value?
Upvotes: 2
Views: 15554
Reputation: 11192
This sample example of ngModelChange :
app.component.html
<p-dropdown [options]="cities" [(ngModel)]="selectedCity" (ngModelChange)="onSelectType($event)"></p-dropdown>
app.component.ts
import { SelectItem } from 'primeng/primeng';
export class AppComponent {
cities: SelectItem[];
selectedCity: any;
previousVal: any;
currentVal: any;
constructor() {
this.cities = [{
"label": "London",
"value": "london"
}, {
"label": "USA",
"value": "usa"
}];
}
onSelectType(event) {
if(event) {
this.previousVal = this.currentVal;
this.currentVal = event;
}
console.log('this.previousVal', this.previousVal);
console.log('this.currentVal', this.currentVal);
}
}
app.module.ts
import { DropdownModule } from 'primeng/primeng';
imports: [ DropdownModule ]
solution is use ngModelChange;
Upvotes: 12