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:
ng generate component all-countries
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:
import { Component, OnInit } from '@angular/core'; import { Country } from '../country'; import { CountryService } from '../country.service'; @Component({ selector: 'app-all-countries', templateUrl: './all-countries.component.html', styleUrls: ['./all-countries.component.css'] }) export class AllCountriesComponent implements OnInit { countries: Country[]; constructor(private countryService: CountryService) { } ngOnInit() { this.getCountries(); } getCountries(): void { this.countries = this.countryService.getCountries(); } }
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.
<div class="container"> <h2>List of All the Countries in Our Database</h2> <a *ngFor="let country of countries" class="country-unit" routerLink="/detail/"> <div class="country-block"> <h4></h4> <p>Capital <br> ()</p> </div> </a> </div>
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:
a { text-decoration: none; } *, *:after, *:before { box-sizing: border-box; } body { font-family: 'Lato'; } h2, h3, h4, p { font-family: 'Lato'; margin: 10px; } .country-block p { margin-top: 0; margin-bottom: 0; } .country-block h4 { margin-bottom: 10px; } h4 { position: relative; font-size: 1.25rem; } .container { margin: 0 50px; text-align: center; } .country-unit { width: 200px; display: inline-block; margin: 10px; } br { clear: both; } .country-block { padding: 30px 0; text-align: center; color: white; height: 150px; background-color: #4CAF50; border-radius: 2px; } .country-block:hover { background-color: #FF5722; cursor: pointer; color:white; }
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:
ng generate component country-detail
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:
import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Location } from '@angular/common'; import { Country } from '../country'; import { CountryService } from '../country.service'; @Component({ selector: 'app-country-detail', templateUrl: './country-detail.component.html', styleUrls: ['./country-detail.component.css'] }) export class CountryDetailComponent implements OnInit { country: Country; constructor( private route: ActivatedRoute, private countryService: CountryService, private location: Location ) { } ngOnInit(): void { this.getCountry(); } getCountry(): void { const name: string = this.route.snapshot.paramMap.get('name'); this.country = this.countryService.getCountry(name); } goBack(): void { this.location.back(); } }
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:
<div class="container"> <div *ngIf="country"> <h2></h2> <p> <span>Capital: </span></p> <p> <span>Area: </span> km <sup>2</sup> </p> <p> <span>Population: </span></p> <p> <span>GDP: </span> USD</p> <p> <span>Currency: </span></p> <button (click)="goBack()">Go Back</button> </div> </div>
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:
.container { margin: 0 auto; width: 380px; } h2, p { font-family: 'Lato'; } p { font-size: 1.25rem; } p span { color: #4CAF50; border-radius: 5px; width: 200px; display: inline-block; } label { display: inline-block; width: 3em; margin: .5em 0; color: #607D8B; font-weight: bold; font-family: 'Lato'; } button { margin-top: 20px; font-family: Arial; background-color: #F44336; border: none; padding: 5px 10px; border-radius: 20px; cursor: pointer; outline: none; color: white; font-family: 'Lato'; }
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.
No comments:
Post a Comment