简体   繁体   English

使用ng-value或ng-model选择时的选择选项

[英]Select option in select using ng-value or ng-model

Currently I am facing this issue 目前我正面临这个问题

I have this array and this select html 我有这个数组,这个选择HTML

$ctrl.languages =
        [
            {
                key: 'Play Prompt',
                value: 'blank'
            },
            {
                key: 'Dont Play Prompt - Assume English',
                value: 'English'
            },
            {
                key: 'Dont Play Prompt - Assume Spanish',
                value: 'Spanish'
            }
        ];

 <select class="form-control" name="vdn" ng-model="$ctrl.ivr.Language"
                                                        ng-options="item.value as item.key for item in $ctrl.languages" ng-required="true">

                                                </select>

I use that dropdown to show the key in the current dropdown for each option, and I save the "value" property of the option selected in a database but when I retrieve the data the option with the "value" ssaved is not selected. 我使用该下拉菜单在每个选项的当前下拉菜单中显示键,并且在数据库中保存了所选选项的“值”属性,但是当我检索数据时,未选择保存了“值”的选项。

In this case if I save 'blank' value in the database I want the dropdown to have the 'Play Prompt' key selected. 在这种情况下,如果我将'blank'值保存在数据库中,则希望下拉列表选择'Play Prompt'键。

Thanks 谢谢

You're setting up item.value as the value that gets set to $ctrl.ivr.Language whenever an option is selected from the dropdown. 您正在将item.value设置为从下拉列表中选择选项时将其设置为$ctrl.ivr.Language的值。 Since the <select> 's ngModel directive is pointing to $ctrl.ivr.Language , setting a value to $ctrl.ivr.Language within the controller will automatically add selected="selectd" to the item in the dropdown that has a value property that has a matching value. 由于<select>ngModel指令指向$ctrl.ivr.Language$ctrl.ivr.Language在控制器内将值设置为$ctrl.ivr.Language会自动将selected="selectd"添加到具有value的下拉菜单项中具有匹配值的属性。

Therefore, if you want "Play Prompt" to be the value that is selected in the dropdown when the database doesn't contain a value for that setting, then you can check whether the value exists in the API response, and if it doesn't, then set $ctrl.ivr.Language = 'blank' to default to the "Play Prompt" option. 因此,如果您希望“播放提示”为当数据库不包含该设置的值时在下拉列表中选择的值,则可以检查该值是否存在于API响应中,如果不存在, t,然后将$ctrl.ivr.Language = 'blank'设为默认为“播放提示”选项。

This looks pretty straightforward. 这看起来非常简单。 I've provided a working plunker here: https://plnkr.co/edit/sCusyXaSJdO0sjgm3csE?p=preview 我在这里提供了一个工作的插件: https ://plnkr.co/edit/sCusyXaSJdO0sjgm3csE ? p = preview

Let me know if you were looking for something else. 让我知道您是否正在寻找其他东西。

In my code, I've added a timeout to simulate a web service call. 在我的代码中,我添加了一个超时来模拟Web服务调用。 Here is the controller code from the plunker: 这是插塞程序的控制器代码:

app.controller('mainController', function($scope, $timeout) {
  $scope.languages = [
      {
          key: 'Play Prompt',
          value: 'blank'
      },
      {
          key: 'Dont Play Prompt - Assume English',
          value: 'English'
      },
      {
          key: 'Dont Play Prompt - Assume Spanish',
          value: 'Spanish'
      }
    ];

    $scope.ivr = {};

    // Simulating Web service Call
    $scope.retrievingData = true;
    $timeout(function() {
      $scope.ivr.Language = 'blank';
      $scope.retrievingData = false;
    }, 2000);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM