【问题标题】:how to make dropdown select a certain value from getting the id from the database?如何使下拉菜单从数据库中获取 id 中选择某个值?
【发布时间】:2020-04-03 23:30:09
【问题描述】:

下面的代码显示了使用 ng-repeat 的下拉列表。当我编辑这个实体时,我必须让它们显示已经选择的某个值,而不仅仅是显示整个列表。我知道当我想选择某个值时,我可以输入 selected 关键字。 那么如果我使用 ng-repeat 填充下拉列表呢?如何使用 id 让下拉菜单选择某个值?

<div ng-if="field.name=='ClienteleId'" class="input-group inputFill">
    <select ng-disabled="defaultSaveButtons[$index]" class="form-control inputFill2"
    ng-model="field.value" ng-options="clientele.id as clientele.name for clientele in     dropdown.clienteleOptions  | orderBy:'name'  track by clientele.id"></select>
 /div>

【问题讨论】:

    标签: angularjs angularjs-ng-repeat angularjs-select


    【解决方案1】:

    要在 AngularJS 中设置 &lt;select&gt; 的值,您需要将设置为 ng-model 的变量更改为选项中的值之一。

    在您的情况下,您将field.value 作为您的ng-model,并且您的选项使用name 属性作为它们的标签,使用id 属性作为它们的值。要默认选中dropdown.clienteleOptions 中的一项,您需要将field.value 设置为dropdown.clienteleOptions 中相应条目的id 属性。

    想象一下您的选项和字段如下所示:

    $scope.clienteleOptions = [
        {id: 1, name: 'test 1'},
        {id: 2, name: 'test 2'}
    ];
    
    $scope.fields = [
        {name: 'ClienteleId', value: null}
        // ...
    ];
    

    在您的控制器某处,您将有这样的东西来选择测试 2:

    $scope.fields[0].value = 2;
    

    或者让它更有活力:

    $scope.fields.forEach(function (field) {
        if (field.name === 'ClienteleId') {
            field.value = 2;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多