简体   繁体   English

用ng-selected预先选择两个下拉列表

[英]pre-selecting two dropdowns with ng-selected in angular

I have two select dropdowns with the first one populating the second one. 我有两个select下拉菜单,第一个填充第二个。 I have the first one pre-selected using ng-selected however I cannot work out how to have the second dropdown selected. 我已经使用ng-selected预先选择了第一个,但是我不知道如何选择第二个下拉菜单。 I have tried using $scope.Colours.options but get error: Cannot read property 'options' of undefined error in console log. 我尝试使用$scope.Colours.options但收到错误: Cannot read property 'options' of undefined error in console log.

    <label for="ColourGroup">Colour Group</label>
    <select id="ColourGroup" class="form-control" ng-model="ColourGroup" ng-options="cgroup as cgroup.a for cgroup in cgroups track by cgroup.v" ng-selected="{{selected.v == cgroup.v}}"></select>


    <label for="Colours">Colour/Woodgrain</label>
    <select id="Colours" class="form-control" ng-model="Colours.options" ng-options="option as option.name for option in ColourGroup.options track by option.id"><option value="">Choose Colour / Woodgrain</option></select>

I have a plunker here: https://plnkr.co/edit/mFdRlX2CjZnsm8EFZMiZ?p=preview 我在这里有一个笨蛋: https ://plnkr.co/edit/mFdRlX2CjZnsm8EFZMiZ ? p = preview

If someone can point me in the right direction that would be great. 如果有人可以指出正确的方向,那就太好了。

Directives ng-init and ng-change can handle it. 指令ng-init和ng-change可以处理它。 Just replace form tag with this one. 只需用此标签替换表单标签即可。 Also you can remove select init at your controller. 您也可以在控制器上删除select init。

<form class="form form-inline" ng-controller="Ctrl">

    <label for="ColourGroup">Colour Group</label>
    <select id="ColourGroup" class="form-control" ng-model="ColourGroup" ng-options="cgroup as cgroup.a for cgroup in cgroups track by cgroup.v" ng-init="ColourGroup=cgroups[0]" ng-change="Colours=ColourGroup.options[0]"></select>


    <label for="Colours">Colour/Woodgrain</label>
    <select id="Colours" class="form-control" ng-model="Colours" ng-options="option as option.name for option in ColourGroup.options track by option.id" ng-init="Colours=ColourGroup.options[0]"></select>

</form>

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

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