【发布时间】:2014-11-30 10:30:30
【问题描述】:
我创建了以下示例,以便您可以准确了解发生了什么:http://jsfiddle.net/8t2Ln/101/
如果我使用 ng-options,也会发生同样的事情。我这样做有不同的原因,但为了简化示例,省略了该部分。
如您所见,默认情况下它有两个选项。我在选择旁边显示了 ng-model 的选定值,以便您可以看到它是什么。当您使用顶部添加第三个选项时,它将值设置为该新选项的值,正如选择旁边显示的 ng-model 值所证明的那样,但选择本身不会更改以显示正确的值已选中。
以下是链接中的示例代码:
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;
};
});
这里是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