How to create Cascading Drop Down (Country and State list) In Angular 6

user10153722 picture user10153722 · Aug 13, 2018 · Viewed 12.9k times · Source

How to create Cascading Drop Down (Country and State list) In Angular 6. I want a fully country and there state list in angular 6.

anyone who know that pl z share your idea.

Answer

Akj picture Akj · Aug 13, 2018

DEMO ----> Cascading Drop Down (Country and State list)

HTML:

<label>Country</label>
<div  title="Please select the country that the customer will primarily be served from">
    <select placeholder="Phantasyland" (change)="changeCountry($event.target.value)">
        <option *ngFor ="let count of countryList">{{count.name}} </option>
    </select>
</div>


<label>City</label>
<div title="Please select the city that the customer is primarily to be served from.">
    <select placeholder="Anycity">
        <option *ngFor ="let city of cities">{{city}} </option>
  </select>
</div>

TS:

countryList: Array<any> = [
    { name: 'Germany', cities: ['Duesseldorf', 'Leinfelden-Echterdingen', 'Eschborn'] },
    { name: 'Spain', cities: ['Barcelona'] },
    { name: 'USA', cities: ['Downers Grove'] },
    { name: 'Mexico', cities: ['Puebla'] },
    { name: 'China', cities: ['Beijing'] },
  ];
  cities: Array<any>;
  changeCountry(count) {
    this.cities = this.countryList.find(con => con.name == count).cities;
  }