Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

February 08 2018


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.


Sonos Controller 8.3.2 (Freeware)

A versatile and useful application that enables you to remotely control your music players and play your favorite songs in all rooms

Quick Eye 8.0 Build 39 / 7.1 Build 38 (Freeware)

View popular file formats in a single environment, such as pictures, audio and video files, HTML pages, CAD files, or text by simply pressing space

novaPDF OEM 9.1 Build 231 (Demo)

Silently install the OEM component, generate PDF files with the help of a virtual printer driver and then process them in your application

February 07 2018

Windows 10 Will Soon Get Progressive Web Apps To Boost the Microsoft Store
EFF Founder John Perry Barlow Has Died At Age 70
Reddit Bans 'Deepfakes' AI Porn Communities
FDA Declares Popular Alt-Medicine Kratom an Opioid
Chinese Companies Hunt for AI Talent at American Conference
Bowing To Popularity, Apple Stores In China Accept Alipay
Now Google Might Make a Game Console and Game-Streaming Service
Estudo não encontra conexão forte entre radiação de celulares e câncer
Apple Watch pode detectar diabetes com 85% de precisão
36 Indicted in Global Cybercrime Ring That Stole $530M
Após críticas, 99 promete resolver problemas em seu novo app
Falha em serviço de VPN vaza localização e IP do usuário
'Humans Not Invited' Is a CAPTCHA Test That Welcomes Bots, Filters Out Humans
Before They Can Drive a Taxi, London's Cabbies Have To Commit the City To Memory in a Rigorous Test Called the Knowledge
Chrome testa recurso LazyLoad para economizar seu 4G
Quais séries os brasileiros maratonaram pela primeira vez na Netflix
Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!