lio*_*far 8 javascript data-binding user-interface local-storage angularjs
我在选择时升级我的ng模型时遇到了问题.
我有以下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>
Run Code Online (Sandbox Code Playgroud)
以下代码:
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'
};
};
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下JSFiddle
我想要做的是在inputDevice中输入第一个设备在集合inputDevices中具有的相同值.
我知道我可以通过elem1然后它会工作但我不能这样做因为我想将选择保存在本地存储中而不是将其恢复到ng-model对象.
任何建议将不胜感激,
谢谢
您可以像 Maxim 演示的那样存储值而不是对象,也可以inputDevices使用以下命令从数组中提取正确的值:
self.inputDevice = self.inputDevices.filter(function(item) {
return item.value == storedValue.value;
})[0];
Run Code Online (Sandbox Code Playgroud)
根据更新的小提琴