javascript - Select an Option in ng-options Not working with jQuery -


i trying select option in ng-options using jquery based on options label being generated based upon text in variable.

$scope.storedday = 'today';  <select id="selectedday" name="selectedday" class="form-control" ng-model="selectedday" ng-options="day.text day in days"></select>  $('#selectedday option[label="' + $scope.storedday + '"]').prop('selected', true); 

generated html

<select id="selectedday" name="selectedday" class="form-control ng-pristine ng-untouched ng-valid" ng-model="selectedday" ng-options="day.text day in days" tabindex="0" aria-invalid="false"> <option value="?" selected="selected"></option> <option value="object:35" label="today">today</option> <option value="object:36" label="tomorrow">tomorrow</option> <option value="object:37" label="sunday">sunday</option> </select> 

it never selected. if hard code in label text.

you can using angular's jqlite iterating on options , comparing labels, see following working fiddle:

//iterate on options (var = 0; < angular.element(document.queryselector('#selectedday').children).length; i++) {     if (angular.element(document.queryselector('#selectedday').children[i]).attr('label') == $scope.storedday) {         //set selected option in ng-model         $scope.selectedday = angular.element(document.queryselector('#selectedday').children[i]).attr('value');     } } 

http://jsfiddle.net/hmartos/sjsk71w8/


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -