1

I have already checked other solution with same problem but nothing worked. I am facing issue in selecting the drop down value as per the ng-model. Below is the code I am using -

<select class="form-control"  name="adminID" 
    ng-model="hospitalsCtrl.hospital.admin_id" 
    ng-disabled="hospitalsCtrl.hospital.ID" 
    ng-options="admin.ID as admin.email  for admin in  hospitalsCtrl.adminsList  track by admin.ID" required>
    <option value="">Please Choose</option>
</select>

Its generating option like below - options being generated

The ng-model value is being populated properly , but the element is not selected. Please help.

Fiddle for this https://jsfiddle.net/6xy03urf/4/

TIA.

6
  • can you create a fiddle for this Commented Sep 22, 2016 at 5:05
  • Maybe you should add some js here, btw is hospitalsCtrl.hospital defined ? Commented Sep 22, 2016 at 5:27
  • Added fiddle, had to add js in the HTML part only, jsfiddle.net/6xy03urf/4 Commented Sep 22, 2016 at 5:28
  • @DMCISSOKHO , yes its defined in the controller and I am able to print the value of hospitalsCtrl.hospital.admin_id as well in the output. You can check the fiddle also, it showing same behaviour. Commented Sep 22, 2016 at 5:31
  • what do you mean by "but the element is not selected" ? it seems to work in your sample Commented Sep 22, 2016 at 5:32

3 Answers 3

3

You should remove the track by admin.ID part from ng-options as it is not allowing to bind with ng-model. Try below code in your HTML

<select class="form-control"  name="adminID" 
    ng-model="hospitalsCtrl.admin_id" 
    ng-options="admin.ID as admin.email  for admin in  hospitalsCtrl.adminsList" required>
    <option value="">Please Choose</option>
</select>

In your JS make a little change like below

     var app = angular.module('App',[]);
app.controller('Hospitals-Controller', function(){

    console.log("ASA");
    var self = this;

     //self.admin_id = 2 ;

    self.adminsList = [
                    { "ID" : 1,
                "name" : "test 1",
              "email" : "[email protected]"

            },
            { "ID" : 2,
                "name" : "test 2",
              "email" : "[email protected]"

            },
             { "ID" : 3,
                "name" : "test 3",
              "email" : "[email protected]"

            }];
                        self.admin_id = self.adminsList[1].ID;
})

Here is the explanation from Angular on how to use select as and track by

Because the selected option has been set programmatically in the controller, the track by expression is also applied to the ngModel value.In the example, the ngModel value is adminsList1.ID and the track by expression evaluates to adminsList1.ID.id (which is undefined). As a result, the model value is not matched against any and the appears as having no selected value

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

6 Comments

Thanks for your answer. If I remove the "track by" , it works in the fiddle but its not working in my app, its exactly the same code. If I use ng-repeat with options it works fine, so I am sticking with that instead of ng-options.
No don't use ng-repeat instead of ng-options ! ng-options has lot of flexibility in case of dropdowns more than ng-repeat.Did you set your ng-model object like I did in answer ?
you mean the last line ? self.admin_id = self.adminsList[1].ID ? this is basically setting the ID value which I have already from the DB, but its not selecting the required value.
Hmm that's hurting :( Okay mate !
I know right! There are things in angular which are remains unexplained :P :P
|
0

You should try like this:

var self = this;

self.adminsList = [
                { "ID" : 1,
            "name" : "test 1",
          "email" : "[email protected]"

        },
        { "ID" : 2,
            "name" : "test 2",
          "email" : "[email protected]"

        },
         { "ID" : 3,
            "name" : "test 3",
          "email" : "[email protected]"

        }];

          self.admin_id = self.adminsList[1];

3 Comments

is there any way to select the option on basis of just Id, not whole object?
why do you want only the id ? this solution don't fit your needs ?
As I do not have whole object I just have id for the field.
0

Both of the answers provided above are valid , but for some reasons its not working for my app. Using ng-repeat for options instead of ng-options is working for me. Posting the code it might help someone with my situation :P . Below is the code which worked for me -

<select class="form-control"  name="adminID" ng-model="hospitalsCtrl.hospital.admin_id" 
ng-disabled="hospitalsCtrl.hospital.ID" required>
    <option value="">Please Choose</option>
    <option ng-repeat="admin in hospitalsCtrl.adminsList" value="{{admin.ID}}">{{admin.email}}</option>
                        </select>

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.