AngularJS - 设置下拉列表中选择值不起作用 [英] AngularJS - setting selected value of dropdown does not work

查看:282
本文介绍了AngularJS - 设置下拉列表中选择值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小提琴这里复制我的问题: http://jsfiddle.net/U3pVM/2840/

正如标题所暗示的,我无法设置的选定值选择使用填充NG选项。

我已经搜索并尝试,我发现所有可能的解决方案。任何帮助或建议将是AP preciated!

HTML

 < D​​IV NG-应用>
    < H2>藤< / H>
    < D​​IV NG控制器=TodoCtrl>
        <选择NG模型=ddlRoomsNG选项=r.id作为r.Name在房间R>
        < /选择>
        {{$ scope.test}}
    < / DIV>
< / DIV>

 函数TodoCtrl($范围){
    $ scope.Rooms = [{名称:'小孩',ID:1},{名称:'其他',ID:2}];
    $ scope.options = [{名称:'其他',ID:2}];
    $ scope.ddlRooms = $ scope.options [0];    $ scope.test =喇嘛;
}


由于你正在做的 id作为名称需要分配的ID给模型:

  $ scope.ddlRooms = $ scope.options [0] .ID;

I made a fiddle replicating my problem here: http://jsfiddle.net/U3pVM/2840/

As the title suggests, I can't set the selected value of a select populated using ng-options.

I have searched and tried all possible solutions that I found. Any help or suggestions would be appreciated!

HTML

<div ng-app>
    <h2>Todo</h2>
    <div ng-controller="TodoCtrl">
        <select ng-model="ddlRooms" ng-options="r.id as r.Name for r in Rooms">
        </select>
        {{$scope.test}}
    </div>
</div>

Angular

function TodoCtrl($scope) {
    $scope.Rooms = [{ Name: 'Toddler', id: 1 },{ Name: 'other', id: 2 }];
    $scope.options = [{ Name: 'other', id: 2 }];
    $scope.ddlRooms = $scope.options[0];

    $scope.test = 'bla';
}

解决方案

Because you are doing id as name you need to assign id to the model:

$scope.ddlRooms = $scope.options[0].id;

这篇关于AngularJS - 设置下拉列表中选择值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆