mf

Creating Your First Angular App: Components, Part 2

In the third tutorial of the series, you learned how to create the HomeComponent of your country information app. We will create two more components in this tutorial. One of the components will list all the countries that we have stored in the COUNTRIES array. Another component will display the details of any country selected by the user.

Creating the AllCountriesComponent

The HomeComponent that we created in the previous tutorial and the AllCountriesComponent that we will create in this section are very similar. The only difference is that instead of sorting the COUNTRIES array and slicing it to only get the first three countries, we will be listing all of them at once. Inside the console, move to your project directory and run the following command:

This will create a folder named all-countries inside the src/app directory of your Angular app. The folder will have three different files named all-countries.component.ts, all-countries.component.html, and all-countries.component.css. The component logic like getting the list of countries and initializing the component itself will go inside the .ts file. The .html file will store the template for the component, and the .css file will store different CSS rules to style the template.

Here is the code for the all-countries.component.ts file:

As you can see, the code is pretty basic. We import the Country and CountryService classes that we created in the second tutorial of the series. The component decorator is used to specify the selector that we will be using to identify the AllCountriesComponent.

Inside the class definition, we create a countries property that accepts an array of Country objects as its value. The CountryService class is added to the component using dependency injection. We call the getCountries() method of this class upon initialization. The getCountries() method itself relies on getCountries() from the CountryService class, which returns an array of Country objects.

The all-countries.component.html file will store the template for our component.

Just like the template for HomeComponent, we are using *ngFor to list all the countries obtained by the getCountries() method and stored in the countries property of the AllCountriesComponent class. We use this component to display the capitals of different countries using the capital property. You will learn about the routerLink directive used with the a tag in the next tutorial.

The CSS used is the same as that of the home.component.css file. The only difference is that we change the background color for each country block to green. Here is the complete CSS code:

Creating the CountryDetailComponent

The CountryDetailComponent will be the third and final component of our Angular app. Whenever users click on the name of any country listed inside either the HomeComponent or AllCountriesComponent, they will be taken to the CountryDetailComponent.

Go back to the console and run the following command:

This will create a folder named country-detail inside the src/app directory of your app. You should see four different files inside the folder. Three of those files will be named: country-detail.component.ts, country-detail.component.html, and country-detail.component.css. Just like the earlier components, country-detail.component.ts will contain the logic of our component, and country-detail.component.html will contain the template to render it.

Here is the code for the country-detail.component.ts file:

This time, we have also imported ActivatedRoute and Location, along with Component and OnInit. We use ActivatedRoute to access information about a route associated with a component loaded in an outlet. We use Location to enable our application to interact with the browser’s URL.

Inside the class definition, we create a property named country which accepts a Country object as its value. Unlike HomeComponent and AllCountriesComponent, the CountryDetailComponent class has to show details of only one country at a time.

The getCountry() method extracts the name parameter from the route snapshot and uses the value to find a country with the given name inside the COUNTRIES array. The goBack() method takes the user back to the previous page with the help of the back() method from the Location class.

Here is the code for the country-detail.component.html file:

The template code inside the div with *ngIf="country" is rendered only if country has been set to a value. We are using Angular pipes to capitalize the name of the country and properly format the area and population of the countries. We are binding the click event of our Go Back button to the goBack() method of our component so that whenever users click on a button, they are taken back to the previous page.

Here is the CSS that will go inside the country-detail.component.css file:

Final Thoughts

With the completion of this tutorial, we have added two more components to our first Angular app. The AllCountriesComponent was very similar to the HomeComponent as they both rendered a list of countries stored in the COUNTRIES array. The CountryDetailComponent was different because it extracted information about a single country from the COUNTRIES array based on its name. 

After creating three different components, you should now have a basic understanding of the interactions between .ts, .html, and .css files to create a fully functioning component.

In the next tutorial of the series, you will learn how to use all these components together and make some final changes so that the app can run without any errors.

Powered by WPeMatico

Leave a Comment

Scroll to Top