Here's what I'm trying to do: I want a select list bound to an array of objects with ngValue, but the first option needs to be a "None" option with a null value.
Model:
this.managers = [
{ id: null, name: "(None)" },
{ id: 1, name: "Jeffrey" },
{ id: 2, name: "Walter" },
{ id: 3, name: "Donnie" }
];
this.employee = {
name: "Maude",
managerId: null
};
View:
<select [(ngModel)]="employee.managerId">
<option *ngFor="#manager of managers" [ngValue]="manager.id">{{ manager.name }}</option>
</select>
On load, the list correctly binds to the "None" element. But if you change to a different item and back, the model value now switches to a string of 0: null. This is pretty inconvenient; it means I have to intercept the value and change it to a null manually before I attempt to save it to the server.
Here's a Plunker with a demo: http://plnkr.co/edit/BH96RWZmvbbO63ZAxgNX?p=preview
This was pretty easily done in Angular 1 with an extra <option value="">None</option>
This seems like it would be a pretty common scenario, and yet I've been unable to find any solutions. I've also tried adding an <option [ngModel]="null">None</option>, but it results in the same 0: null value.
Any suggestions?