0

I am using PrimeNG to render my dynamic dropdown options. Based on the option value I don't want my ngModel to be udpated.

Let's say if my option is an object such as dropdownObject.canUpdateModel = true, then I want my ngModel to be updated, if not ngModel should not be updated. Can someone please help me with this.

<p-dropdown #dp [options]="myList"(onChange)="onFMyDropdownChange(selectedForeCast)"[(ngModel)]="selectedForeCast"></p-dropdown>
Sriks
  • 1,669
  • 6
  • 26
  • 40

1 Answers1

2

Use getter/setter approach, so you will be able to intercept attempt to set new value to model - inspect the value for given criteria - and assignt to model or reject the change.

It would be something like this

get selectedForeCast(){
   return whateverModelValue;
}

set selectedForeCast(selectedValue){
   if(selectedValue whatever here){
     whateverModelValue=selectedValue;
 }
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Thanks @Antoniossss for your reply. I have implemented as below. But if I select the option as per below else condition, first time the debugger comes to else block which is expected. But if I change the option and come back to the else conditioned option, the control is not coming to setter. get selectedForeCast() { return this.selectedForecast; } set selectedForeCast(selectedForecast) { if (selectedForecast.status !== 'P') { this.selectedForecast = selectedForecast; } else { this.showGenerateScenarioStatus(); } } – Sriks Apr 30 '19 at 19:47
  • Maybe exception in showGenerateScenarioStatus. Cannot tell without code. Stackblitz. But anyway, this wraps up the main question here. This is the way to go. – Antoniossss Apr 30 '19 at 20:08