1

I want to display by default a value from a json which is something like

[{"city":"SURAT"},{"city":"BARODA"},{"city":"AHMEDABAD"},{"city":"MUMBAI"}]

this is came from server. I am store this value in $scope.user_all_city and there is one city is provided to user which is i am stored in this

$scope.user_city = "surat";

now my select tag is something like this

<select ng-model="model.user_all_city" ng-options="rm.city for rm in user_all_city" ng-init="model.user_all_city=user_city">

it is display first value as a blank. it is return to me default city when i am print {{mode.user_all_city}} but it is didn't display as selected option in it.

please help me over here and how to call a function in when i am change the value.

Gautam Golakiya
  • 453
  • 2
  • 12

1 Answers1

1

Try using ng-repeat instead of ng-options basically the ng-options is not that much descriptive in angularjs.

  <select ng-model="cityName" required>
<option>Select city</option>
<option ng-selected="cityName==city" value="{{city}}" ng-repeat="city in cityList"></option>
</select>

here the required attribute will enusre that select city is highlighted in case of null value. and ng-selected will check if that expression is true and the only that object is selected.The value inseide the model=cityName play the role of selected value here.