I currently have a service in angular returning me a list of states. The array looks like this:
model.Address.StateList = [
{ Disabled: false,
Group: null,
Selected: false,
Text: "CA",
Value: "1"
},
{ Disabled: false,
Group: null,
Selected: false,
Text: "IL",
Value: "2"
},
{ Disabled: false,
Group: null,
Selected: false,
Text: "NJ",
Value: "3"
}
]
I am also returning from the service the state they actually choose which looks like this:
model.Address.State = "CA"
I have scoured stackoverflow trying to find a way to make this work but I can't figure out how to pre-select the value in the list.
I have tried this:
<select id="State" ng-model="model.Address.State" ng-change="changeState(model.Address.State)" ng-options="option as option.Text for option in model.Address.StateList track by option.Value"></select>
But that does not pre-select the first item.
The one way I have got it to work is changing ng-model to "selectedItem" and in the controller I wrote:
$scope.selectedItem = responseModel.Address.StateList[0];
But that only picks the first item.
It seems that the problem is that I have a collection of States but the model I want is just the singular text. And ideally I'd like to set the selected to be added to the model object so when I save I can just pass "model" to my api with all the values I need.