1

Hello all I want to show selected value in dropdown based on the value fetch from the database in angularjs. I am trying the basic functionality where I am setting ng-model value to the dropdown after fetch it from database. But it is not working for me. Its always show me blank dropdown.

My dropdown structure

 <select name="singleSelect" ng-model="measurements.Backzip" class="form-control">
                                <option value="">--Select --</option>
                                <option ng-repeat="sel in selectables" value="{{sel.value}}">{{sel.label}}</option>
                            </select>

My code for fetch the data from database and assign value to measurements.Backzip

Firstly I have declared the variables

 $scope.measurements = {};
$scope.measurements.Backzip = "";

Then After this I create this function

 function GetDetails() {

        $scope.selectables = [
       { label: 'Yes', value: 'true'},
       { label: 'NO', value: 'false' } 
        ];

        $scope.getemail = store.get('usersession');

        var productResult = customerService.getMeasurementDetails($scope.getemail);
        productResult.then(function (result) {
            if (!result.data) {
                console.log("empty");
            }
            else { 
                store.set("mvalues", result.data);
                $scope.measurements = result.data;
                $scope.measurements.Backzip = result.data.Backzip;
                console.log("zip",result.data.Backzip);

            }
        });
    };

Json output from my api

{"id":2,"userid":null,"Height":3,"Kameezlength":3,"Shoulder":3,"SleeveLength":3,"SleeveMoori":3,"Armhole"
:3,"Chest":3,"Waist":3,"Hip":3,"KameezGhera":3,"Chalk":3,"FrontNeck":3,"BackNeck":1,"BackDori":3,"Backzip"
:false,"Plates":3,"SalwarLength":3,"SalwarHalfPatiala":null,"SalwarFullPatiala":null,"SalwarPouncha"
:3,"SalwarAssan":3,"SalwarBelt":3,"PazamiLength":32,"PazamiChurridaar":null,"PazamiThigh":3,"PazamiKnee"
:3,"PazamiMoori":3,"PazamiAssan":3,"PazamiBelt":3,"useremail":"[email protected]"}

Data is fetching succesfully here.But I am unable to assign the selected value to dropdown. Can anybody help. I tried almost every example from stackoveflow. But nothing works. Maybe I am missing anything

5
  • Try to manually set the value and see if that changes it: $scope.measurements.Backzip = 'true'; Commented Mar 16, 2016 at 18:22
  • yes it is changing @QuetiM.Porta Commented Mar 16, 2016 at 18:24
  • So it's probably related to what you are getting back from the server Commented Mar 16, 2016 at 18:25
  • in sqlserver its getting data from bit datatype column its true and false @QuetiM.Porta Commented Mar 16, 2016 at 18:27
  • Take a look at @Lex's answer. You are getting back a boolean value, but in the $scope.selectables, you've defined the value as a string. Simply define the value in $scope.selectables without the quotes and see if that works. Commented Mar 16, 2016 at 18:28

1 Answer 1

1

This is the classic issue of trying to assign a value to a select that is not a string. Fortunately, Angular has the perfect construct to address this in ng-options.

Change your HTML to this:

<select 
    name="singleSelect"
    ng-model="measurements.Backzip"
    class="form-control"
    ng-options="option.value as option.label for option in selectables">
</select>

And then change the code in your controller to this:

$scope.selectables = [
    { label: '--Select--'},
    { label: 'Yes', value: true},
    { label: 'NO', value: false }];

** Update: Added the default '--Select--' option from the original.

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

6 Comments

Not working how? Perhaps the value of measurements.Backzip is not a boolean?
its boolean. from sqlserver bit type column its getting values
Can you post the value of result.data?
check I update my question with the output which I am getting from api @QuetiM.Porta
Try removing the $scope.measurements.Backzip = ""; line from your controller.
|

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.