Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  109] [ 7]  / answers: 1 / hits: 15626  / 11 Years ago, sun, december 22, 2013, 12:00:00

I'm facing a problem in upgrading my ng-model in selection.

I have the following 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>


And the following code:



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'
};
};
}


You can use the following JSFiddle



What I want to do is put in inputDevice the same values that the first device has in the collection inputDevices.

I know that I can pass elem1 and it will work however i can't do it since i want to save the selection in Local Storage and than restore it to the ng-model object.

Any suggestion will be grateful

Thanks


More From » angularjs

 Answers
12

You can either store the value instead of the object as Maxim has demonstrated, or you can pull the correct value from the inputDevices array with something like:



self.inputDevice = self.inputDevices.filter(function(item) {
return item.value == storedValue.value;
})[0];


as per an updated fiddle


[#73597] Saturday, December 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tobyl

Total Points: 598
Total Questions: 110
Total Answers: 114

Location: Vietnam
Member since Sat, Feb 12, 2022
2 Years ago
tobyl questions
Tue, Aug 10, 21, 00:00, 3 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
;