Angularjs:在更新 ng-model 时选择不更新 [英] Angularjs: select not updating when ng-model is updated

查看:25
本文介绍了Angularjs:在更新 ng-model 时选择不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下示例,以便您可以确切地看到发生了什么: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 = [{值:'1',标签:'输入1'},{值:'2',标签:'输入2'}];$scope.selectedDevice = '';$scope.addType = 函数 () {var newElem = {标签:$scope.newInput,值:'3'};$scope.inputDevice.push(newElem);$scope.selectedDevice = newElem.value;};});

这是html:

<div ng-controller="Ctrl"><p><input type="text" ng-model="newInput"/><br/><button ng-click="addType()">添加类型</button></p><选择 ng-model="selectedDevice"><选项></选项><option ng-repeat="i in inputDevice" value="{{ i.value }}">{{ i.label }} - {{ i.value }}</option></选择>{{ selectedDevice }}

解决方案

这正是您不应该使用 ngRepeat 来呈现选择选项的原因.你应该使用 ngOptions 代替:


                
            
发送“验证码”获取 | 15天全站免登陆