如何使用angular JS显示基于另一个选择的选择值

时间:2015-08-07 09:17:50

标签: angularjs

我在选择框中有位置列表。在选择位置时,相应的事件类型应显示在另一个选择框中。

$scope.locationNames=[
            {id:"Onboard",value:"On-board service"},
            {id:"Clubhouse",value:"Clubhouse"},
            {id:"Gate",value:"Gate"}
    ];

    $scope.incidentTypesList={
            Onboard:[
                    {id:"IFE faulty",value:"IFE faulty"},
                    {id:"223",value:"No special meal as ordered"},
                    {id:"Spoilt",value:"Spoilt/damaged belongings"}
            ];

            Clubhouse:[
                    {id:"",value"No appointments available"},
                    {id:"",value="Late/delayed transport service"},
                    {id:"",value="Facilities not available"}
            ];
    };

我可以使用以下代码在列表中获取位置名称。

<select class="firstDropDown" ng-model="location" ng-options="item.id as item.value for item in locationNames">
<option value="">Select location</option>
</select>

您能否帮我解决如何选择此选项以在另一个选择框中显示值。

1 个答案:

答案 0 :(得分:4)

我认为以下解决方案符合您的需求。您的阵列中存在一些需要修复的语法错误。

<强> HTML

<div ng-app="myApp" ng-controller="myAppCtrl">
    <select class="firstDropDown" ng-model="location" ng-options="item.id as item.value for item in locationNames">
        <option value="">Select location</option>
    </select>
    <select class="secondDropDown" ng-model="incident" ng-options="incident.id as incident.value for incident in incidentTypesList[location]">
        <option value="">Select incident</option>
    </select>
</div>

JS

var myApp = angular.module("myApp", [])

myApp.controller("myAppCtrl", function($scope){
    $scope.locationNames=[
        {id:"Onboard",value:"On-board service"},
        {id:"Clubhouse",value:"Clubhouse"},
        {id:"Gate",value:"Gate"}
    ];

    $scope.incidentTypesList={
        Onboard:[
            {id:"IFE faulty",value:"IFE faulty"},
            {id:"223",value:"No special meal as ordered"},
            {id:"Spoilt",value:"Spoilt/damaged belongings"}
        ], 
        Clubhouse:[
            {id:"",value:"No appointments available"},
            {id:"",value:"Late/delayed transport service"},
            {id:"",value:"Facilities not available"}
        ]
    }   
});

JSFiddle:enter image description here