默认设置下&;&选择GT;异步数据值 [英] Setting default <select> value with asynchronous data
问题描述
我有一个下拉列表
<select ng-model="referral.organization"
ng-options="key as value for (key, value) in organizations">
</select>
其中,组织
正在使用$ http请求填补。我也有一个资源推荐
其中包括一些特性,包括一个整数对应保存在降值组织
-下。目前,下拉工作正常,并选择一个新的值将更新我的推荐
没有问题的资源。
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.
然而,当页面加载的下拉是空白,而不是显示的 referral.organization
这是从服务器检索(即打开时,该值previously保存推荐
形式)。我明白,这可能是由于我的资源为空时,第一次加载页面,但我要如何更新下拉当信息被成功取回?
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?
编辑: {{组织[referral.organization]}}
成功地列出所选值,如果放在别的地方在页面上,但我不知道如何给这个标签的前pression显示。
{{ 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.
第二个编辑:
问题显然是在ngOptions使用的密钥,并在ngModel中使用的变量之间的不匹配。在&LT;选择&GT;
选项的正在返回从的WebAPI字符串(尽管开始时是作为字典),而推荐
模式是返回整数。当 referral.organization
被安排在ngModel,角不匹配2723为2723等字样。
Second
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.
我尝试了几种不同的东西,但是下面的作品好,是最干净给我。在回调为$资源GET,我简单地改变为字符串必需的变量,像这样:
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);
...
});
不知道这会被认为是与角(不匹配1000为1000)或的WebAPI(序列词典℃的问题,INT,字符串&GT;
到 {关键:值}
),但这是功能和&LT;选择&GT;
标签仍然绑定到推荐
资源。
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.
推荐答案
其他答案是,它通常是正确的只是工作。问题是最终存储在 $ scope.organizations
在组织
键(整数)和键之间的不匹配存储在 $ HTTP
的反应,这是存储在JSON,因此作为一个字符串。角不匹配字符串的整数。正如我在我的编辑原来的问题提到的,当时的解决办法是在 $ HTTP
响应数据转换为字符串。我不知道如果Angular.js的当前版本还表现这种方式。
The other answers were correct in that it usually "just works." The issue was ultimately a mismatch between the organization
key (an integer) stored inside $scope.organizations
and the key as stored in the $http
response, which is stored in JSON and therefore as a string. Angular was not matching the string to the integer. As I mentioned in my edit to the original question, the solution at the time was to cast the $http
response data to a string. I am not sure if current versions of Angular.js still behave this way.
这篇关于默认设置下&;&选择GT;异步数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!