0

This is my angular with JQuery

mainDodApp.controller('EditPageController', ['$scope', '$http', '$sce', function ($scope, $http, $sce) {
    $http({
        //HTTP Call
    }).success(function (response) {
        $scope.parent_page = $sce.trustAsHtml(response);
    });

    $http({
        //HTTP Call
    }).then(function (response) {
        $scope.formData.parent_page_id = response.data.parent_page_id;
        $('#parentPage option[value='+$scope.formData.parent_page_id+']').attr('selected','selected');
    });

}]);

and this is my HTML.

<select name="parent_pages" id="parentPage" ng-bind-html="parent_page" ng-model="formData.parent_page_id">
</select>

as i am rendering options from ajax call into ng-bind-html and when in second ajax call after getting value for selected option i am selecting option and it is selected also like

<option value="2" selected="selected">About Us</option>

but does not show as default selected value although it has selected attribute. and jquery version is jQuery v1.11.2

hu7sy
  • 983
  • 1
  • 13
  • 47

1 Answers1

0

Try 'prop' function and be sure $scope.formData.parent_page_id return value.

  $('#parentPage option[value='+$scope.formData.parent_page_id+']').prop('selected', true);

Sample fiddle using jQuery v1.11.2

4b0
  • 21,981
  • 30
  • 95
  • 142
  • yes i am assure it returned value but solution you give does not work it only work for $('#parentPage option[value='+$scope.formData.parent_page_id+']').attr('selected','selected'); and i can see selected option in DOM as well – hu7sy Jan 12 '17 at 05:00
  • and it is jQuery v1.11.2 – hu7sy Jan 12 '17 at 05:03