I have a drop down list
<select ng-model="referral.organization" ng-options="key as value for (key, value) in organizations"> </select>
where organizations populated using the $ http request. I also have a referral resource that includes several properties, including an organization integer corresponding to the value stored in the drop-down list. Currently the dropdown menu works fine and choosing a new value will update my referral resource without any problems.
However, when the page loads, the drop-down list is empty, and the value of referral.organization that was extracted from the server (that is, when opening the previously saved referral form) is not displayed. I understand that this is probably due to the fact that my resource is empty when the first page loads, but how can I update the drop-down list when the information has been successfully restored?
Edit: {{ organizations[referral.organization] }} successfully displays the selected value if it is placed somewhere else on the page, but I donโt know how to specify a tag to display this expression.
Second edit: The problem was apparently a mismatch between the key used in ngOptions and the variable used in ngModel. The <select> option was returned as strings from the WebAPI (despite the start of the dictionary), while the referral model returned integers. When referral.organization was placed in ngModel, Angular did not match 2723 and "2723", etc.
I tried several different things, but the following works well and is the โcleanestโ for me. In the callback for the $ resource GET, I simply change the necessary variables to strings as follows:
$scope.referral = $resource("some/resource").get(function (data) { data.organization = String(data.organization); ... });
Not sure if this will be considered a problem with Angular (doesn't match 1000 by "1000") or WebAPI (serialization Dictionary<int,String> to { "key":"value" } ), but it's functional, and the <select> still linked to the referral resource.
angularjs
Joshua dutton
source share