I've managed to get a Select list to bind with my model for the purpose of saving, but I cannot work out how to make Angular2 automatically select the correct option on the Select list if I'm providing editing functionality. In other words, if I'm editing a pre-existing object via a form, I need the Select list to reflect the initial state of the object (e.g. option 5 in the select list), rather than it just defaulting to the first item.
<select [ngModel]="originalObject">
<option *ngFor="let object of objects" [ngValue]="object">{{object.name}}</option>
</select>
How I imagine it should work, but doesn't!
<select [ngModel]="originalObject">
<option *ngFor="let object of objects" [ngValue]="object" [selected]="object === originalObject">{{object.name}}</option>
</select>
So essentially I'm trying to make use of the 'selected' property on option, but for whatever reason it doesn't do anything. The 'selectedObject' in this case would be an object in the component that it can read.
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
In reactive form we can use setValue and patchValue of FormGroup and in template-driven form we can use ngModel to set value in select box dynamically. We can also use 'selected' attribute in <option> tag of select element to set default value selected in select box.
For that Angular provides [ngValue] directive. Now as country is an object, in our template we need to display country.name for SELECT OPTIONS and also use [ngValue] directive as we are passing an object as a value. Also to provide a default value we need to set one of objects from countries array. });
First, create the template variables for dropdown using #teams . Use the event binding to listen for the change event and link with the onSelect method. The onSelect method receives the reference variable and takes the value of the dropdown element. Next, create the onSelected(value) method in the TypeScript file.
Okay, so I figured out what the problem was, and the approach I believe works best. In my case, because the two objects weren't identical from a Javascript perspective, as in: they may have shared the same values, but they were different actual objects, e.g. originalObject
was instantiated entirely separately from objects
which was essentially an array of reference data (to populate the dropdown).
I found that the approach that worked best for me was to compare a unique property of the objects, rather than directly compare the two entire objects. This comparison is done in the bound property selected
:
<select [ngModel]="originalObject">
<option *ngFor="let object of objects" [ngValue]="object" [selected]="object.uniqueId === originalObject.uniqueId">{{object.name}}</option>
</select>
Update to angular 4.X.X, there is a new way to mark an option selected:
<select [compareWith]="byId" [(ngModel)]="selectedItem">
<option *ngFor="let item of items" [ngValue]="item">{{item.name}}
</option>
</select>
byId(item1: ItemModel, item2: ItemModel) {
return item1.id === item2.id;
}
Some tutorial here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With