【问题标题】:selected option on angularjs dropdownangularjs下拉列表中的选定选项
【发布时间】:2018-04-05 15:29:33
【问题描述】:

有些东西让我头疼。

我在一个网络应用程序上使用 angularjs,我用 BD 结果构建了一个表,每一行都有一个选择项,女巫加载了一个模型,但是,我不知道如何标记特定选项,具体取决于结果项。

我的代码:

var ctrlScope = $scope;
ctrlScope.antenas = [];

// Model to select
ctrlScope.motivos = [ 
    {id:0,descripcion:"--  Seleccion una opcion  --"},       
    {id:1,descripcion:"Corte de Luz"},
    {id:2,descripcion:"Falla de proveedor"},
    {id:3,descripcion:"Mantenimiento"},
    {id:4,descripcion:"Micro corte"},
    {id:5,descripcion:"Otro"},
    {id:6,descripcion:"Por definir"}
]


// Method to load data
call_url = app_root+'Auditoria/reporte';
    var dsd = $('#desde').val();
    var has = $('#hasta').val();

    $http.post(call_url,{desde:dsd, hasta:has})
    .then(function(resp){
        var data = resp.data
        for(var item in data){
            ctrlScope.antenas.push({
                "ip": data[item].ip,
                "nombre":data[item].nombre,
                "nodo": data[item].nodo,
                "fecha": data[item].fecha,
                "motivo": data[item].motivo
            });
        }//for
    }) //then

HTML:

<tr ng-repeat="ant in antenas">
        <td>{{ ant.ip }}</td>
        <td>{{ ant.nombre }}</td>
        <td>{{ ant.nodo }}</td>
        <td>{{ ant.fecha }}</td>
        <td>
            <select class="form-control">
                <option 
                    ng-repeat="motiv in motivos" 
                    ng-selected="{{ ant.motivo }}" 
                    value="{{ motiv.id }}">{{ motiv.descripcion }}
                </option>
                </select>
            </td>
        </tr>

我尝试使用 ng-selected 但不起作用。

【问题讨论】:

  • 您希望默认选择哪个选项?
  • 你试过ng-selected="ant.motivo"而不是ng-selected="{{ ant.motivo }}"。 ant.motive 是真/假或空/非空或已定义/未定义的变量吗?

标签: javascript php angularjs


【解决方案1】:

我只是假设ant.motivo 应该等于motiv.id。如果是这种情况,您的选项标签应如下所示:

<option ng-repeat="motiv in motivos"
        ng-value="motiv.id"
        ng-selected="motiv.id == ant.motivo"
        ng-bind="motiv.descripcion"></option>

ng-selected 需要一个为真或假的表达式。虽然你可以使用ng-valueng-bind

【讨论】:

  • 非常感谢,我没看到。
【解决方案2】:

你必须使用 ngOptions 试试这个

<select ng-model="ant.motivo"
        ng-options="motiv.id as motiv.descripcion for motiv in motivos">
</select> 

看这里 https://plnkr.co/edit/8ZhWqRy12BwCGXxEeI5u?p=preview

【讨论】:

  • 谢谢,它也可以工作,这是最好的方法,因为它是下拉菜单的特定指令。
猜你喜欢
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
相关资源
最近更新 更多