0

I have a controller defined as

 var searchController = function ($scope, $http) {
    $scope.modSelectedState = null;
    $scope.states = [];
    $http.get('./data/states.js')
       .success(function (data) {
           $scope.states = data.locations;
       })
}

Its job for now is to populate the list of states which I consume (successfully) from a local file.

Then, I have this code in my HTML file:

 <select ng-model="modSelectedState" id="selectState" name="selectState"
         ng-options="state as state.name for state in states">
          <option value="">Select state...</option>
 </select>

Which I cannot get to work. I know that my controller is defined properly and that the array is being populated successfully because i tried the following piece of code and it worked fine:

<ul ng-repeat="state in states">
       <li>{{state.name}}</li>
 </ul>

I placed this code right before the options code under the scope of the same controller of course.

A JSON object in my array looks like this:

   {
        "lat": 35.02499,
        "long": -84.92153,
        "name": "Alabama",
        "short": "AL"
    }

Cannot figure out why it is not working..

1

1 Answer 1

2

Instead of

ng-options="state as state.name for state in states"

try

ng-options="state.name for state in states"

Regarding modSelectedState try the following:

...
   .success(function (data) {
       $scope.states = data.locations;
       $scope.modSelectedState = $scope.states[1];  // 1 is just an arbitrary element
'''

See Angular docs and examples.

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

11 Comments

Thanks. I tried your suggestion but it didn't work. Also, I have been using the same code in a few other projects and it works fine just as it is, and that's the weird thing about all this.
"Did not work" is not very informative. Any errors? Have you tried debugging?
Sorry about that. Didn't work means that I was not able to populate the list. I don't see any errors in the debugger.
Have you tried to populate modSelectedState (not leave it null)?
No I haven't. Normally I don't even set it to null and it is working. What are you suggesting?
|

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.