I am trying to select an option in ng-options using jquery based on the options label being generated based upon the text in a variable.
$scope.storedDay = 'Today';
<select id="selectedDay" name="selectedDay" class="form-control" ng-model="selectedDay" ng-options="day.text for 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 for 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 is never selected. Even if I hard code in the label text.
答案 0 :(得分:0)
您可以使用angular的jqLite迭代选项并比较它们的标签来执行此操作,请参阅以下工作小提琴:
//Iterate over options
for (var i = 0; i < 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');
}
}
答案 1 :(得分:0)
我只需要在ng-options = FAIL:}
中添加track by day.text