I created the following example so that you can see exactly what is happening: http://jsfiddle.net/8t2Ln/101/
The same thing happens if I use ng-options. I have another reason for this, but to simplify the example, this part came out.
As you can see, by default it has two options. I am showing the selected ng model value next to select so you can see what it is. When you use the top to add a third option, it sets the value to the value of this new option, as evidenced by the displayed value of the ng model next to the selection, but the selection itself does not change to show the correct value is selected.
Below is an example of the code from the link:
var testApp = angular.module('testApp', ['ngRoute']); testApp.controller('Ctrl', function ($scope) { $scope.newInput = ''; $scope.inputDevice = [ { value: '1', label: 'input1' }, { value: '2', label: 'input2' } ]; $scope.selectedDevice = ''; $scope.addType = function () { var newElem = { label: $scope.newInput, value: '3' }; $scope.inputDevice.push(newElem); $scope.selectedDevice = newElem.value; }; });
And here is the html:
<div ng-app="testApp"> <div ng-controller="Ctrl"> <p> <input type="text" ng-model="newInput" /> <br /> <button ng-click="addType()">Add Type</button> </p> <select ng-model="selectedDevice"> <option></option> <option ng-repeat="i in inputDevice" value="{{ i.value }}">{{ i.label }} - {{ i.value }}</option> </select> {{ selectedDevice }}</div> </div>
javascript angularjs angular-ngmodel
Jhorra Oct 06 '14 at 7:25 2014-10-06 07:25
source share