0

I have few dropdowns with predefined field. Now i am making ajax call to database and fetch data. Data is coming properly from the database and i can see the array in firebug(network tab).

I want to set this data as selected in dropdown. In textbox it is simple. But i having hard time with dropdowns.

Ajax code below

formApp.controller('getprofile', function($scope,$http){
     $http({
                            url: 'get_profile.php',
                            method: "GET",
                            params: {uid: uid}
                         })
                    .success(function(data) {

                        if (data.success) {

                      $scope.formData.dob = data.dob;
                      $scope.married = data.married;
}

Html code

<input name="dob" id="dob" type="text" class="form-control textbox1" required="required" placeholder="Date of birth(dd-mm-yyyy) " ng-model="formData.dob">
<div class = "errorba" ng-show="dob">{{dob}}</div>
</div>

<div class="form-group" ng-class="{ 'has-error' : errormarried }">    
        <select id="married" name="married" class="selector form-control" ng-model="formData.married" required="required"> 

          <option value="0" selected="selected" >Maritial Status</option>
          <option value="1" >Single</option>
          <option value="2">Married</option>

        </select>
<span class="errorba" ng-show="errormarried">{{ errormarried }}</span>
</div>

Data from ajax - 1 1 means Single.

Please advise what am i doing wrong.

5
  • Share the response you get from the API call. Commented Jan 31, 2017 at 4:50
  • 1
    change $scope.married = data.married; to $scope.formData.married = data.married; in controller since your ng-model and $scope variable in your controller is different Commented Jan 31, 2017 at 4:51
  • @nivas it worked. Thank you. Please paste it in answer. So i can accept it. Commented Jan 31, 2017 at 4:55
  • okay, i have done it. Commented Jan 31, 2017 at 4:58
  • Better to use ng-options with ng-selected Commented Jan 31, 2017 at 4:58

1 Answer 1

1

change $scope.married = data.married; to $scope.formData.married = data.married; in controller since your ng-model and $scope variable in your controller is different.

In worst case you may get error formData as undefined in your controller. so define $scope.formData = {}; before you use assign values to it.

formApp.controller('getprofile', function($scope,$http){
     $http({
                            url: 'get_profile.php',
                            method: "GET",
                            params: {uid: uid}
                         })
                    .success(function(data) {

                        if (data.success) {
                      $scope.formData = {};
                      $scope.formData.dob = data.dob;
                      $scope.formData.married = data.married;
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.