I have a drop-down list
<select ng-model="referral.organization"
ng-options="key as value for (key, value) in organizations">
</select>
where organizations
is filled using a $http request. I also have a resource referral
which includes several properties, including an integer organization
that corresponds to the value saved in the drop-down. Currently, the drop-down works fine and selecting a new value will update my referral
resource without issue.
However, when the page loads the drop-down is blank rather than displaying the value of referral.organization
that was retrieved from the server (that is, when opening a previously saved referral
form). I understand that this is likely due to my resource being empty when the page first loads, but how do I update the drop-down when the information has been successfully retrieved?
Edit:
{{ organizations[referral.organization] }}
successfully lists the selected value if placed somewhere else on the page, but I do not know how to give the tag this expression to display.
Second Edit:
The problem was apparently a mismatch between the key used in ngOptions and the variable used in ngModel. The <select>
option's were being returned as strings from WebAPI (despite beginning as Dictionary) while the referral
model was returning integers. When referral.organization
was placed in ngModel, Angular was not matching 2723 to "2723" and so forth.
I tried several different things, but the following works well and is the "cleanest" to me. In the callback for the $resource GET, I simply change the necessary variables to strings like so:
$scope.referral = $resource("some/resource").get(function (data) {
data.organization = String(data.organization);
...
});
Not sure if this would be considered a problem with Angular (not matching 1000 to "1000") or WebAPI (serializing Dictionary<int,String>
to { "key":"value" }
) but this is functional and the <select>
tag is still bound to the referral
resource.