Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 9, 2018 01:00 pm

Create a Library Finder App in Angular: LibraryListComponent and LibraryDetailsComponent

In the previous tutorial of our series, we completed the creation of our HomeComponent and started working on the LibraryListComponent. While the HomeComponent allowed the users to enter the name of any library and search for it, the LibraryListComponent provided a list of libraries which matched the given search term.

So far, we have only written the logic of our LibraryListComponent—we still need to update the HTML template and CSS files to make this component useful. In this tutorial, we will update the remaining files of our LibraryListComponent and also create the LibraryDetailsComponent in its entirety.

Creating the LibraryListComponent Template

If you read the previous tutorial, you might remember that we stored the data of individual libraries as objects in separate array elements. Since we plan to show all these results to our users, we need to use the *ngFor repeater directive to go over the whole list and render them inside the LibraryListComponent.

Each library will have its own containing div block with the name of the library rendered inside h4 tags. At the bottom of each library, there is a link which takes users to the details page of that library. All these elements are wrapped inside a container div with a fixed width of 800 px.

Here is the CSS that I have used to style the elements inside the LibraryListComponent template. I have added a top border on each library unit so that they look separate from each other. You can use another technique to separate them:

Creating the LibraryDetailsComponent

The LibraryDetailsComponent is the last component of our app. To quickly generate all the necessary files, move to your project directory and run the following command from the console.

This will create a folder called library-details in the root directory of your app with four files in it. We only need to be concerned with the three files named library-details.component.ts, library-details.component.html, and library-details.component.css.

Let's begin editing the library-details.component.ts file first. It will contain all the logic of our component. Just like LibraryListComponent, we begin by importing different dependencies.

One additional dependency that we will import is Location. It allows us to interact with the browser's URL. We will be using it to allow our readers to go back to the previous page by clicking on the back button inside our LibraryDetailsComponent. If they arrived here through the LibraryListComponent, they will be taken back to the list of libraries. If they arrived here by clicking on any of the popular libraries on the HomeComponent, they will be taken back to HomeComponent.

Inside the LibraryDetailsComponent class definition, we declare a bunch of properties to store information like the latest version, description, homepage, license, etc. All these have been initialized with a value of "Loading...". This value will be updated as soon as we get back the data about a particular library.

We call the getLibrary() method of our LibraryDetailsComponent upon initialization so that the relevant values can be populated as quickly as possible. Here is the complete code of our library-details.component.ts file:

Creating the LibraryDetailsComponent Template and Stylesheet

We have already stored all the information about a library in different variables, so displaying it to users will be easy now. I have used additional span tags in the template to show the information to users. This allowed me to properly align these values with ease. Here is the code for the library-details.component.html file:

Just like other components, I have wrapped all the elements inside a container div this time as well.

We will set the width of all span elements with the title class to have a fixed width of 250 px. This way, the details of the library will be aligned properly. Here is the CSS that needs to go inside our library-details.component.css file:

Final Thoughts

We began this tutorial by updating the template and CSS file of our LibraryListComponent. After that, we moved on to the LibraryDetailsComponent and learned how to display all the information about a particular library to our users. After completing all the four tutorials in this series, you should now have a working library finder app. 

The fastest way to learn Angular is to do something all by yourself. Keeping this in mind, you should try making some changes to this app. For instance, you could limit LibraryListComponent to only showing the first 20 or so results, or sort those results based on the name of the library, etc. We did something similar in our first Angular app series. Combining the knowledge of both these series should help you make these changes with ease.

I hope this series improved your understanding of Angular. If there is anything that you would like me to clarify in this tutorial, let me know in the comments.


Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code