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?
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 ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.