如何通过ng-model选择ng-option

时间:2017-03-20 14:59:45

标签: angularjs angularjs-ng-repeat ng-options

我有一张桌子,我用ng-repeat填充。例如,让我们说我创建了一个evite类型的模拟网站,该表显示了每个响应过的人。每行都有一个单选按钮,选中后,它将在表格下方的div中填充其他信息。此div还用作回复和更新现有回复的模板。当我从上表中选择一行时,将填充此div中的字段。其中一个字段是选择下拉框,使用ng-options填充。这些选项显示" YES"," NO"和" MAYBE"但是当将响应发送回服务时,它需要一个代码编号" 1"," 2"或" 3"分别。我的ng-options看起来像这样:

ng-options="decision.decisionCode as decision.decisionDescription for decision in vm.decisionList"

我的问题是,当我从表中选择一行时,我无法显示该选项。我尝试将select的ng-model设置为decisionCode,decisionDescription和object,但下拉列表仍为空白。如何为选择指定ng-model以显示表中的选定值?

<div class="col-md-2 ">
    <select class="form-control" id="decision" ng-model="vm.decisionSelected" ng-options="decision.decisionCode as decision.decisionDescription for decision in vm.decisionList"></select>
</div>

vm.decisionList:

[{
"decisionCode": "1",
"decisionDescription": "YES"
}, {
"decisionCode": "2",
"decisionDescription": "NO"
}, {
"decisionCode": "3",
"decisionDescription": "MAYBE"
}]

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码,它对我来说很好。您应该关注的一件事是如何设置控制器。我假设您正在使用Controller As语法考虑您的代码段,因此您的控制器必须至少看起来像这样:

Task<IEnumerable<Customer>> GetCustomers(string url)
{
  HttpClient client = new HttpClient();
  HttpResponseMessage response = await client.GetAsync(url);
  response.EnsureSuccessStatusCode();
  string responseBody = await response.Content.ReadAsStringAsync();

  // TODO: Deserialize responseBody and build a new IEnumerable<Customer>
}

Json:

{
    nextRecords: '\customers\123'
    customers: [
        {
            name: 'John Doe'
        },
        {
            name: 'Mary Doe'
        }
        ]
}

如果不是这样,你正在使vm.decisionList成为$ scope变量,因此它是$ scope.decisionList,它不会被Controller As语法识别,所以你需要删除&#34; vm &#34;在您的ng-options