1

This question was asked many times and couldn't help me with my problem. Not sure whats wrong with the approach and hope some fresh "eyes"

Here is my dropdownlist binding in HTML: I am binding programName which is a string to both value and text properties.

<select [ngModel]="selectedObject">
   <option *ngFor="let p of programs" value= {{p.programName}}>
         {{p.programName}}
   </option>
</select>

In my .ts file, here is the what i am doing to bind the default value to the "select"

 loadData(pList: IProgram[]) {
    this.programs = pList;
    if (this.programs.length > 0) {
        this.selectedObject = this.programs[0];
    }
}

The data is binding to "select" properly,

  • but the default value is showing as empty text, where i need to click and select an value.
  • Another issue is when i change the dropdown value from A to B, the "selectedObject" value is always giving me "A" instead of "B". As it is [ngModel], two way binding would take care. But, its not happening as i might be missing something here.

Can someone help whats the problem with this approach?

2 Answers 2

1

try below,

<select [(ngModel)]="selectedObject">
   <option *ngFor="let p of programs" 
           [ngValue] = "p"
           [selected]="p.programName == selectedObject.programName"
          >
         {{p.programName}}
   </option>
</select>

Check this Plunker!!

Hope this helps!!

Sign up to request clarification or add additional context in comments.

9 Comments

No, it didn't work with this fix. Even after this, the default value is empty and changed value was still showing as first value "A" instead of "B".
@Born2Code Ah, value binding was off too, it should be [value] = "p", rather than value= {{p.programName}}, Cheers!!
Thanks Madhu. That fixed my first issue of defaulting the value instead of empty selection. The second issue still exists, I was doing below code to load the selected value getmyData() { this._myService.getPayments(this.selectedObject.programName) .subscribe(pl => this.loadmyData(pl), error => this.errorMessage = <any>error, () => this.showLoader = false); }
Updated Plunker, use ngValue instead.
Thanks again ! I have updated the plunker and validated, with this <h3>selected value: {{selectedObject.programName}}</h3> and still see the selected value as A when selected with "B". Wondering if (onChange) needs to be added? In other case, it should not as the binding already exists.
|
0

Basically, the problem is when declaring [ngModel]. It should be within [(ngModel)] which was missed in my original problem. :)

Thanks a lot to Madhu Ranjan for help on the fix !

After correcting (ngModel) able to bind and fetch successfully.

<select [**(ngModel)**]="selectedObject"><option *ngFor="let p of programs"value= {{p.programName}}>{{p.programName}}</option> </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.