Anand Yerrapati
Anand Yerrapati

Reputation: 115

How to show values of a select based on another select using angular JS

I have list of locations in a select box. On selection of a location, the corresponding incidenttypes should be displayed in another select box.

$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"}
            ];
    };

I am able to get location names in the list using the below code.

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

Can you please help me how to implement on selection on this to show the values in another select box.

Upvotes: 3

Views: 3013

Answers (1)

ArBro
ArBro

Reputation: 731

I think the solution below fits your needs. There were some syntax errors in your array which needed to be fixed.

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: https://jsfiddle.net/ABr/wqy2ha9o/3/

Upvotes: 4

Related Questions