Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  133] [ 4]  / answers: 1 / hits: 17582  / 8 Years ago, thu, january 12, 2017, 12:00:00

I have the following dropdown. I want to set All Patients as the default value.



    <select [(ngModel)]=searchModel.careprovider>
<option [value]=0>All Pateints</option>
<option *ngFor=let user of practiceUsers [value]=user._id.$oid>
{{user.dn}}
</option>
</select>


My model is declared this way:



searchModel: any = { location: null, practice: null, name: '', careProvider: 0 };


I set the practiceUsers this way:



  this._practice.getUsers(this.searchModel.practice).subscribe(result => {
this.practiceUsers = result;
this.searchModel.careProvider = 0;
});


No matter how I change it I always just get a blank option as the default. I've tried adding an object to the this.practiceUsers array after it is loaded, then setting the model value. I've tried setting the model value with and without quotes to see if a number or string made a difference. Everything I try still results in the default being the blank option.



In Angular 1 I would have used ng-options, but that is no longer available for Angular 2, and every example I find shows to use the ngFor for dropdowns.


More From » angular

 Answers
17

Object attributes are case sensitive, in your object, attribute is called careProvider, but in your template, you are using searchModel.careprovider with lowercase p. I think you also have to use NgValue directive instead of value because you are using NgModel directive. So, this should work: it is not working



<select [(ngModel)]=searchModel.careProvider>
<option [ngValue]=0>All Pateints</option>
<option *ngFor=let user of practiceUsers [ngValue]=user._id.$oid>
{{user.dn}}
</option>
</select>

[#59375] Tuesday, January 10, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
angelr

Total Points: 399
Total Questions: 96
Total Answers: 101

Location: Finland
Member since Sun, May 21, 2023
1 Year ago
;