Cascading DropDown List using Angular 2

For demo, this angular 2 application will load the list of states in state dropdown based on selected country. Before we continue further, I strongly recommend you to read the post how to bind dropdown/select list in angular 2 to understand code and various new angular 2 concepts used in this application as there is no point repeating the same content here. So as you saw in post, the sample angular 2 application for binding the country list has following structure.

And addition to above files, there are a couple of new files added to application and modified some files. We will each of them in detail. First, let’s visit newly added files.

state.ts: This file has a simple class called “States” with 3 properties id, countryid and name.

dataservice.ts: This is an Angular 2 service which returns a list of countries and states.

Code Walk-through

State.ts

DataService.ts

Till Angular 1.x, there are different ways to define a service like factory, service and provider. But with Angular 2, the only way is via classes. Read more about Angular 1.x and Angular 2 difference here. The Angular service can take the data from JSON file, Web API, Web Service or any other local storage. The consumer is not aware of how service is getting the data. So for our service, there is a class defined “DataService” which has 2 methods that returns Countries and States array. Remember, previously we had countries array in Countrylistcomponent.ts file. But as a best practice, it should be part of service as in future if we have to change the way we capture the data there is no need to modify the component or consumer.

Notice, the very first import statement and @Injectable() function as decorator. TypeScript sees the @Injectable() decorator and emits metadata about our service, metadata that Angular may need to inject other dependencies into this service.

Our service doesn’t have any dependencies at the moment. So removing these 2 statements will not break your code but let’s add the decorator anyway. From Angular 2 docs,

It is a “best practice” to apply the @Injectable() decorator ​from the start​ for consistency and for future-proofing.

CountryListComponent.ts

This file is modified to use the service to get countries and states list.

First, import the Service and then within @Component directive, set providers: [DataService].

And inject the service in CountryListComponent constructor. And in constructor, call getCountries() method to get list of countries and populate countries variable.

Also defined onSelect method which will be called onChange event of country dropdown list. This method gets the list of states from our service and then using filter, filter states based on selected country.

CountryListComponent.html

This is the file specified in the templateURL for @Component directive and contains HTML code. There are 2 dropdowns list one for country and other for state and also change event defined on country dropdown list. Angular 2 has different way of defining event. Take the HTML event and wrap it with parentheses. $event.target.value will give the selected country id.

Demo

If you noticed there is functionality that when country is selected, the “–Select–” option from state dropdown is no longer available. So we need to hide the default option whenever any country is selected.

Thank you for sharing.
Would you please tell me how to code the “DataService.ts” if I want to take the data from JSON file?
for example :
the country.json like this [{“code”:”1″,”name”:”USA”},{“code”:”2″,”name”:”India”},…..]
and the state.json like this [{“code”:”101″,”countryCode”:”1″,”name”:”Arizona”},{“code”:”201″,”countryCode”:”2″,”name”:”Gujarat”},……]