Angular 5 Set selected value of HTML Select Element
You can select an item of the dropdown list by setting the value of property.manager
. Assuming that selectedName
is the name of the Manager
item that you want to select, you can do this:
// Case sensitive
this.property.manager = this.managers.find(m => m.name === this.selectedName);
// Case insensitive
let selectedNameUp = this.selectedName.toUpperCase();
this.property.manager = this.managers.find(m => m.name.toUpperCase() === selectedNameUp);
Here are the relevant parts of the markup and code. See this stackblitz for a demo.
HTML:
<select name="manager" [(ngModel)]="property.manager" class="form-control" required>
<option disabled [ngValue]="undefined">Select Manager</option>
<option *ngFor="let manager of managers" [ngValue]="manager">{{manager.name}}</option>
</select>
<input type="text" [(ngModel)]="selectedName" (ngModelChange)="onNameChange($event)">
Code:
selectedName: string;
property = {
ref_no: '',
address: '',
manager: undefined
};
managers = [
{ "company": "Test Company", "name": "John Doe", "id": "3oE37Fo2QxGHw52W7UHI" },
{ "company": "Another Company", "name": "John Brown", "id": "LRF8xAi48rRuWu0KZex3" },
{ "company": "XYZ", "name": "Subhan Ahmed", "id": "TqOQHbdwJdwgwD8Oej8v" }
];
onNameChange($event) {
let selectedNameUp = this.selectedName.toUpperCase();
this.property.manager = this.managers.find(m => m.name.toUpperCase() === selectedNameUp);
}
Subhan
Updated on May 12, 2020Comments
-
Subhan about 4 years
Here is what I'm trying to do:
<select name="manager" id="manager" [(ngModel)]="property.manager" class="form-control" (change)="onChangeManager($event)" required> <option disabled value="">Select Manager</option> <option *ngFor="let manager of managers" [ngValue]="manager" [selected]="manager?.name === 'Subhan Ahmed'"> {{manager?.name}} </option> </select>
What I need is when the view is initialised, I need to set the value of the select where
manager?.name == property.manager.name
(which is loaded from db on on another event). I've tried to place a default textSubhan Ahmed
to select the default value but its not working.Managers are loaded at the start, I load them from Firestore and assign them to a variable
managers: Observable<Manager>;
duringsubscribe()
, whileproperty.manager
is loaded after another input's change event.Am i missing something?
-
Subhan about 6 yearsYes It does have an ID generated by Firestore which I snapshot along with the data.
-
Subhan about 6 yearsstackblitz.com/edit/angular-coraqv Have a look at this scenario. Type a name in input and hit tab. The select box should show you the selected value.
-
Subhan about 6 yearsstackblitz.com/edit/… Have a look now.
-
ConnorsFan about 6 yearsI updated my answer, and included the stackblitz adapted from yours.
-
Subhan about 6 yearsNo, In my scenario, its not working. I'll update the stackblitz with more code. I'm just missing something I recon. Your answer was really helpful by the way. I'll accept it as an answer as I get things working so that others can use a correct answer for future references. Thanks
-
Subhan about 6 yearsYour solution works. It was some issue with how I implemented it in my scenario. Thanks you.