Cascading DropDown In Angular 2

To know about the basics and how to get started with Angular 2, please read the following articles first and add mandatory files and npm_modules to your application.

Getting Started

  • Start Visual Studio.
  • Create a new website.
  • Provide the name and the location of website.
  • Click "Next". 

Once all the files are added and npm_module is installed, let’s work on how to show data.

First of all, add two components and make some properties with data type.

Country.ts

  1. export class Country {  
  2.     constructor(  
  3.         public id: number,  
  4.         public name: string) {}  
  5. }   

State.ts

  1. export class State {  
  2.     constructor(  
  3.         public id: number,  
  4.         public countryid: number,  
  5.         public name: string) {}  
  6. } 

Now, add a component for data and provide name like CountryService. Add some data for Country and States.

  1. import {  
  2.     Injectable  
  3. } from '@angular/core';  
  4. import {  
  5.     Country  
  6. } from './country';  
  7. import {  
  8.     State  
  9. } from './state';  
  10. @Injectable()  
  11. export class CountryService {  
  12.     getCountries() {  
  13.         return [  
  14.             new Country(1, 'United States'),  
  15.             new Country(2, 'India'),  
  16.             new Country(3, 'Australia'),  
  17.             new Country(4, 'New Zealand'),  
  18.             new Country(5, 'South Africa'),  
  19.             new Country(6, 'United Kingdom')  
  20.         ];  
  21.     }  
  22.     getStates() {  
  23.         return [  
  24.             new State(1, 1, 'Alabama'),  
  25.             new State(2, 1, 'Alaska'),  
  26.             new State(3, 1, 'Arizona'),  
  27.             new State(5, 1, 'Arkansas'),  
  28.             new State(6, 1, 'California'),  
  29.             new State(7, 1, 'Colorado'),  
  30.             new State(8, 1, 'Connecticut'),  
  31.             new State(9, 1, 'Delaware'),  
  32.             new State(10, 1, 'Florida'),  
  33.             new State(11, 1, 'Georgia'),  
  34.             new State(12, 1, 'Hawaii'),  
  35.             new State(13, 1, 'Idaho'),  
  36.             new State(14, 1, 'Illinois'),  
  37.             new State(15, 1, 'Indiana'),  
  38.             new State(16, 2, 'New Delhi'),  
  39.             new State(17, 2, 'Maharashtra'),  
  40.             new State(18, 2, 'Goa'),  
  41.             new State(19, 2, 'Punjab'),  
  42.             new State(20, 2, 'Haryana'),  
  43.             new State(21, 2, 'Uttar Pradesh'),  
  44.             new State(22, 2, 'Rajasthan'),  
  45.             new State(23, 2, 'Andhra Pradesh'),  
  46.             new State(24, 2, 'Jharkhand'),  
  47.             new State(25, 2, 'Madhya Pradesh'),  
  48.             new State(26, 3, 'New South Wales'),  
  49.             new State(27, 3, 'Tasmania'),  
  50.             new State(28, 3, 'Qweensland'),  
  51.             new State(29, 3, 'Western Australia'),  
  52.             new State(30, 3, 'Victoria'),  
  53.             new State(31, 4, 'Auckland'),  
  54.             new State(32, 4, 'Wellington'),  
  55.             new State(33, 4, 'Christchurch'),  
  56.             new State(34, 4, 'Hamilton'),  
  57.             new State(35, 4, 'Napier'),  
  58.             new State(31, 5, 'Eastern Cape'),  
  59.             new State(32, 5, 'Limpopo'),  
  60.             new State(33, 5, 'Mpumalanga'),  
  61.             new State(34, 5, 'North West'),  
  62.             new State(35, 5, 'Northern Cape'),  
  63.             new State(31, 6, 'Herefordshire'),  
  64.             new State(32, 6, 'Durham'),  
  65.             new State(33, 6, 'Manchester'),  
  66.             new State(34, 6, 'South Yorkshire'),  
  67.             new State(35, 6, 'Birmingham')  
  68.         ];  
  69.     }  
  70. }   

Add a new component and reference for country, state, and data service.

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. import {  
  5.     CountryService  
  6. } from './shared/countryservice';  
  7. import {  
  8.     Country  
  9. } from './shared/country';  
  10. import {  
  11.     State  
  12. } from './shared/state';  
  13. @Component({  
  14.     selector: 'country-component',  
  15.     templateUrl: 'app/countrycomponent.html',  
  16.     providers: [CountryService]  
  17. })  
  18. export class CountryComponent {  
  19.     selectedCountry: Country = new Country(0, 'India');  
  20.     countries: Country[];  
  21.     states: State[];  
  22.     constructor(private _countryService: CountryService) {  
  23.         this.countries = this._countryService.getCountries();  
  24.     }  
  25.     onSelect(countryid) {  
  26.         this.states = this._countryService.getStates().filter((item) => item.countryid == countryid);  
  27.     }  
  28. }   

Here is my HTML code.

  1. <label>Country:</label>  
  2. <select [(ngModel)]="selectedCountry.id" (change)="onSelect($event.target.value)">  
  3. <option value="0">--Select Country--</option>  
  4. <option *ngFor="let country of countries" value={{country.id}}>{{country.name}}</option>  
  5. </select>  
  6. <br /><br />  
  7. <div>  
  8.     <label>State:</label>  
  9.     <select>  
  10. <option *ngIf='selectedCountry.id == 0' value="0">--Select State--</option>  
  11. <option *ngFor="let state of states " value={{state.id}}>{{state.name}}</option>  
  12. </select>  
  13. </div>   

Now, add reference for the component and data service on module component, like the following.

  1. import {  
  2.     NgModule  
  3. } from '@angular/core';  
  4. import {  
  5.     BrowserModule  
  6. } from '@angular/platform-browser';  
  7. import {  
  8.     FormsModule  
  9. } from '@angular/forms';  
  10. import {  
  11.     HttpModule  
  12. } from '@angular/http';  
  13. import {  
  14.     CountryComponent  
  15. } from './countrycomponent';  
  16. import {  
  17.     CountryService  
  18. } from './shared/countryservice';  
  19. @NgModule({  
  20.     imports: [BrowserModule, FormsModule, HttpModule],  
  21.     declarations: [CountryComponent],  
  22.     providers: [CountryService],  
  23.     bootstrap: [CountryComponent]  
  24. })  
  25. export class AppModule {};  
Now, let’s work on default and add mandatory Angular files and add components.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title></title>  
  6.     <meta charset="utf-8" />  
  7.     <meta charset="utf-8" />  
  8.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  9.     <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />  
  10.     <link href="app/css/styles.css" rel="stylesheet" />  
  11.     <!-- Polyfill(s) for older browsers -->  
  12.     <script src="node_modules/core-js/client/shim.min.js"></script>  
  13.     <script src="node_modules/zone.js/dist/zone.js"></script>  
  14.     <script src="node_modules/reflect-metadata/Reflect.js"></script>  
  15.     <script src="node_modules/systemjs/dist/system.src.js"></script>  
  16.     <!-- Configure SystemJS -->  
  17.     <script src="systemjs.config.js"></script>  
  18.     <script>  
  19.         System.import('app').catch(  
  20.             function(err) {  
  21.                 console.error(err);  
  22.             });  
  23.     </script>  
  24. </head>  
  25.   
  26. <body>  
  27.     <header class="navbar navbar-inner navbar-fixed-top">  
  28.         <nav class="container">  
  29.             <div class="navbar-header">  
  30.                 <span class="app-title">Cascading DropDown in Angular 2</span>  
  31.             </div>  
  32.         </nav>  
  33.     </header>  
  34.     <main class="container">  
  35.         <country-component>Loading...</country-component>  
  36.     </main>  
  37. </body>  
  38.   
  39. </html>   

Run the application.

Select a country to see the states list.

Conclusion

In this article, we have learned how to use cascading DropDown in Angular 2. If you have any questions or comments, drop me a comment in the comments section.