我在选择中升级我的 ng-model 时遇到问题。
我有以下 HTML:
<div ng-app>
<div ng-controller="Ctrl">
<select ng-model="viewmodel.inputDevice"
ng-options="i.label for i in viewmodel.inputDevices">
</select>
</div>
</div>
和下面的代码:
function Ctrl($scope) {
// view model
$scope.viewmodel = new function () {
var self = this;
var elem1 = {
value: '1',
label: 'input1'
};
var elem2 = {
value: '2',
label: 'input2'
}
self.inputDevices = [elem1, elem2];
self.inputDevice = {
value: '1',
label: 'input1'
};
};
}
您可以使用以下 JSFiddle
我想要做的是将第一个设备在集合 inputDevices 中的相同值放入 inputDevice。
我知道我可以传递 elem1 并且它会起作用但是我不能这样做因为我想将选择保存在本地存储中而不是将它恢复到 ng-model 对象。
任何建议将不胜感激
谢谢
最佳答案
您可以像 Maxim 演示的那样存储值而不是对象,或者您可以从 inputDevices
数组中提取正确的值,例如:
self.inputDevice = self.inputDevices.filter(function(item) {
return item.value == storedValue.value;
})[0];
关于javascript - AngularJS 选择 - 在 Controller 中设置 ng-model 不会更新所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20731549/