Learn Components in Angular for Beginners - Part Three

Introduction

 
In this article, we will learn about components in Angular applications. This article is the continuation of my previous article that is  "Learn Angular's Folder Structure & Execution Flow For Beginners - Part Two". Before reading this article, please read my previous article for your easy understanding.
 
Whenever we create a new Angular application, by default a component will be created that is AppComponent which is a parent or root component.
 
By default, the application will come as a component, i.e. appcomponent within the src/app folder.
 
Learn Components In Angular For Beginners
 
Each component contains files such as:
  • app.component.html
  • app.component.css
  • app.component.spec.ts
  • app.component.ts
From the above files, we will write the HTML related code in app.component.html, CSS code in app.component.css, app.component.spec.ts file is used for testing the component functionality and business logic in app.component.ts.
 
In app.Component.ts we have @Component decorator class which instructs that this is not a normal class, but an angular class. @component contains metadata such as a selector, templateUrl, and styleUrls, if we want to write the application inline then we can write the HTML and CSS code in the same app.component.ts by using template and styles.
 
Selector
 
Here, the Selector contains the value from where the components can be rendered, each selector will have a unique name, we can provide our own selector name, by default app.component.ts selector will come with 'app-root'. If we want to navigate from one component to another component, we use the selector value for rendering. For example, if we want to navigate from the parent component to the child component. In the parent component app.component.html file, we use the child selector for rendering the child component.
 
templateUrl
 
The templateUrl contains the app.component.html file of the component, where we will write the HTML code.
 
styleUrls
 
The styleUrls contains the app.component.css file of the component, where we will write the CSS code, which will be applied only for the current component. If we are working with Inline code we use the below,
 
template
 
In this, we define the complete HTML code by using the below syntax.
 
Syntax
  1. template: `<h1>It is a Inline statement</h1>`    
Styles
 
In this, we define the complete CSS code by using the below syntax.
 
Syntax
  1. styles: ['table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th { border: 3px solid #1a1717; text-align: left;padding: 8px;}']  
If we want to create a new component, we will use the below command.
  • ng generate component Test
  • ng g c Test
Let's create a new component with ng generate component Test command.
 
Learn Components In Angular For Beginners
 
Create a new component with ng g c Test1 command.
 
Learn Components In Angular For Beginners
 
When we create a component say Test and Test1, it will be reflected in src/app folder with name Test, Test1 components folders.
 
Learn Components In Angular For Beginners
 
The entries or registry will be added in app.module.ts folder, like below.
 
Learn Components In Angular For Beginners
 
Let's see an example of a component, open the app.component.ts file and place in the below code.
  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.  templateUrl: './app.component.html',  
  6.   styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9.   EmployeeId = 1702356;  
  10.   FirstName = 'Khaja';  
  11.   LastName = 'Moizuddin';  
  12.   EmployeeName: string = this.FirstName + this.LastName;  
  13.   EmployeeDesignation = 'Software Engineer';  
  14.   DateOfJoining: Date = new Date('Mon Apr 22 2016 07:44:57');  
  15.   EmployeeAddress = 'Malakpet, Hyderabad';  
  16.   EmployeePhone = 8019804474;  
  17.   EmployeeAge = 29;  
  18.   Salary  = 20000;  
  19.   City = 'Hyderabad';  
  20.   CompanyName = 'Legato Health Technologies';  
  21.   
  22.   employeeList: any[] = [  
  23.      {  
  24.    EmployeeId : 103621,  
  25.    EmployeeName : 'Khaja Moizuddin',  
  26.    EmployeeDesignation : 'Software Engineer',  
  27.    DateOfJoining : new Date('Mon Apr 22 2016 10:30:57'),  
  28.    EmployeeAddress : 'Gacchibowli, Hyderabad',  
  29.    EmployeePhone : 8019804474,  
  30.    EmployeeAge : 29,  
  31.    Salary : 10000,  
  32.    City : 'Hyderabad',  
  33.    CompanyName : 'Legato'  
  34.   },  
  35.   {  
  36.     EmployeeId : 103622,  
  37.     EmployeeName : 'Pavan Kumar',  
  38.     EmployeeDesignation : 'S.Software Engineer',  
  39.     DateOfJoining : new Date('Tue Jul 19 2012 09:27:45'),  
  40.     EmployeeAddress : 'DLF City, Bangalore',  
  41.     EmployeePhone : 9030344566,  
  42.     EmployeeAge : 25,  
  43.     Salary : 15000,  
  44.     City : 'Bangalore',  
  45.     CompanyName : 'Infosys'  
  46.   },  
  47.   {  
  48.     EmployeeId : 165893,  
  49.     EmployeeName : 'Vikas',  
  50.     EmployeeDesignation : 'S.Software Engineer',  
  51.     DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),  
  52.     EmployeeAddress : 'Electronic City, Bangalore',  
  53.     EmployeePhone : 9030344566,  
  54.     EmployeeAge : 23,  
  55.     Salary : 20000,  
  56.     City : 'Bangalore',  
  57.     CompanyName : 'TCS'  
  58.   },  
  59.   {  
  60.     EmployeeId : 1045896,  
  61.     EmployeeName : 'Vivek Sharma',  
  62.     EmployeeDesignation : 'Delivery Manager',  
  63.     DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),  
  64.     EmployeeAddress : 'Madhapur, Hyderabad',  
  65.     EmployeePhone : 9030344566,  
  66.     EmployeeAge : 23,  
  67.     Salary : 20000,  
  68.     City : 'Hyderabad',  
  69.     CompanyName : 'Microsoft'  
  70.   }  
  71. ];  
  72. }  
Here, in the above code, we are creating employeeList object with type as any[]. The same object we use in our HTML code for rendering to HTML table.
 
Now open the app.component.html file and place the below code.
  1. <h2>Components in Angular - Employee</h2>    
  2. <hr />    
  3.     
  4. <table>    
  5.     <tr>    
  6.         <th>EmployeeId</th>    
  7.         <th>EmployeeName</th>    
  8.         <th>EmployeeDesignation</th>    
  9.         <th>DateOfJoining</th>    
  10.         <th>EmployeeAddress</th>    
  11.         <th>EmployeePhone</th>    
  12.         <th>EmployeeAge</th>    
  13.         <th>Salary</th>    
  14.         <th>City</th>    
  15.         <th>CompanyName</th>    
  16.     </tr>    
  17.     
  18.     <tr>    
  19.       <td>{{this.EmployeeId}} </td>    
  20.       <td>{{this.EmployeeName}}</td>    
  21.       <td>{{this.EmployeeDesignation}}</td>    
  22.       <td>{{this.DateOfJoining}}</td>    
  23.       <td>{{this.EmployeeAddress}}</td>    
  24.       <td>{{this.EmployeePhone}}</td>    
  25.       <td>{{this.EmployeeAge}}</td>    
  26.       <td>{{this.Salary | currency: 'USD'true}}</td>    
  27.       <td>{{this.City}}</td>    
  28.       <td>{{this.CompanyName}}</td>    
  29.     </tr>    
  30.     </table><br><br>    
  31.     
  32.     <h2>Components in Angular - EmployeeList</h2>    
  33.     <hr>    
  34.     <table>    
  35.       <tr>    
  36.         <th>EmployeeId</th>    
  37.         <th>EmployeeName</th>    
  38.         <th>EmployeeDesignation</th>    
  39.         <th>DateOfJoining</th>    
  40.         <th>EmployeeAddress</th>    
  41.         <th>EmployeePhone</th>    
  42.         <th>EmployeeAge</th>    
  43.         <th>Salary</th>    
  44.         <th>City</th>    
  45.         <th>CompanyName</th>    
  46.       </tr>    
  47.     
  48.     
  49.       <tr *ngFor = 'let employee of employeeList'>    
  50.         <td>{{employee.EmployeeId}} </td>    
  51.         <td>{{employee.EmployeeName}}</td>    
  52.         <td>{{employee.EmployeeDesignation}}</td>    
  53.         <td>{{employee.DateOfJoining}}</td>    
  54.         <td>{{employee.EmployeeAddress}}</td>    
  55.         <td>{{employee.EmployeePhone}}</td>    
  56.         <td>{{employee.EmployeeAge}}</td>    
  57.         <td>{{employee.Salary | currency: 'USD'true}}</td>    
  58.         <td>{{employee.City}}</td>    
  59.         <td>{{employee.CompanyName}}</td>    
  60.       </tr>    
  61.     </table>     
Here, we are using two HTML table to render the data from app.component.ts. In the second HTML table, to iterate from a list of employee objects we used, ngFor directive, which works similar to a Foreach loop. Here, we will iterate through the collection of employeeList and display each employee in HTML table.
 
Now open the app.component.css file and place in the below code.
  1. table {  
  2.   font-familyarialsans-serif;  
  3.   border-collapsecollapse;  
  4.   width100%;  
  5. }  
  6.   
  7. td, th {  
  8.   border3px solid #1a1717;  
  9.   text-alignleft;  
  10.   padding8px;  
  11. }  
If we want to work with the inline code we need to keep all the HTML, CSS and Typescript code in only one file that is app.component.ts. Comment the code in app.component.html & app.component.css files. Here, we use @component metadata such as selector, template and styles whenever working with inline code. Open the app.component.ts and place the below code.
  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.   template: `  
  6. <h2>Components in Angular - Employee</h2>  
  7. <hr />  
  8.   
  9. <table>  
  10.     <tr>  
  11.         <th>EmployeeId</th>  
  12.         <th>EmployeeName</th>  
  13.         <th>EmployeeDesignation</th>  
  14.         <th>DateOfJoining</th>  
  15.         <th>EmployeeAddress</th>  
  16.         <th>EmployeePhone</th>  
  17.         <th>EmployeeAge</th>  
  18.         <th>Salary</th>  
  19.         <th>City</th>  
  20.         <th>CompanyName</th>  
  21.     </tr>  
  22.   
  23.     <tr>  
  24.       <td>{{this.EmployeeId}} </td>  
  25.       <td>{{this.EmployeeName}}</td>  
  26.       <td>{{this.EmployeeDesignation}}</td>  
  27.       <td>{{this.DateOfJoining}}</td>  
  28.       <td>{{this.EmployeeAddress}}</td>  
  29.       <td>{{this.EmployeePhone}}</td>  
  30.       <td>{{this.EmployeeAge}}</td>  
  31.       <td>{{this.Salary | currency: 'USD'true}}</td>  
  32.       <td>{{this.City}}</td>  
  33.       <td>{{this.CompanyName}}</td>  
  34.     </tr>  
  35.     </table><br><br>  
  36.   
  37.     <h2>Components in Angular - EmployeeList</h2>  
  38.     <hr>  
  39.     <table>  
  40.       <tr>  
  41.         <th>EmployeeId</th>  
  42.         <th>EmployeeName</th>  
  43.         <th>EmployeeDesignation</th>  
  44.         <th>DateOfJoining</th>  
  45.         <th>EmployeeAddress</th>  
  46.         <th>EmployeePhone</th>  
  47.         <th>EmployeeAge</th>  
  48.         <th>Salary</th>  
  49.         <th>City</th>  
  50.         <th>CompanyName</th>  
  51.       </tr>  
  52.   
  53.   
  54.       <tr *ngFor = 'let employee of employeeList'>  
  55.         <td>{{employee.EmployeeId}} </td>  
  56.         <td>{{employee.EmployeeName}}</td>  
  57.         <td>{{employee.EmployeeDesignation}}</td>  
  58.         <td>{{employee.DateOfJoining}}</td>  
  59.         <td>{{employee.EmployeeAddress}}</td>  
  60.         <td>{{employee.EmployeePhone}}</td>  
  61.         <td>{{employee.EmployeeAge}}</td>  
  62.         <td>{{employee.Salary | currency: 'USD'true}}</td>  
  63.         <td>{{employee.City}}</td>  
  64.         <td>{{employee.CompanyName}}</td>  
  65.       </tr>  
  66.     </table>`,  
  67.   styles: ['table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th { border: 3px solid #1a1717; text-align: left;padding: 8px;}']  
  68.   })  
  69.   
  70. export class AppComponent {  
  71.   EmployeeId = 1702356;  
  72.   FirstName = 'Khaja';  
  73.   LastName = 'Moizuddin';  
  74.   EmployeeName: string = this.FirstName + this.LastName;  
  75.   EmployeeDesignation = 'Software Engineer';  
  76.   DateOfJoining: Date = new Date('Mon Apr 22 2016 07:44:57');  
  77.   EmployeeAddress = 'Malakpet, Hyderabad';  
  78.   EmployeePhone = 8019804474;  
  79.   EmployeeAge = 29;  
  80.   Salary  = 20000;  
  81.   City = 'Hyderabad';  
  82.   CompanyName = 'Legato Health Technologies';  
  83.   
  84.   employeeList: any[] = [  
  85.      {  
  86.    EmployeeId : 103621,  
  87.    EmployeeName : 'Khaja Moizuddin',  
  88.    EmployeeDesignation : 'Software Engineer',  
  89.    DateOfJoining : new Date('Mon Apr 22 2016 10:30:57'),  
  90.    EmployeeAddress : 'Gacchibowli, Hyderabad',  
  91.    EmployeePhone : 8019804474,  
  92.    EmployeeAge : 29,  
  93.    Salary : 10000,  
  94.    City : 'Hyderabad',  
  95.    CompanyName : 'Legato'  
  96.   },  
  97.   {  
  98.     EmployeeId : 103622,  
  99.     EmployeeName : 'Pavan Kumar',  
  100.     EmployeeDesignation : 'S.Software Engineer',  
  101.     DateOfJoining : new Date('Tue Jul 19 2012 09:27:45'),  
  102.     EmployeeAddress : 'DLF City, Bangalore',  
  103.     EmployeePhone : 9030344566,  
  104.     EmployeeAge : 25,  
  105.     Salary : 15000,  
  106.     City : 'Bangalore',  
  107.     CompanyName : 'Infosys'  
  108.   },  
  109.   {  
  110.     EmployeeId : 165893,  
  111.     EmployeeName : 'Vikas',  
  112.     EmployeeDesignation : 'S.Software Engineer',  
  113.     DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),  
  114.     EmployeeAddress : 'Electronic City, Bangalore',  
  115.     EmployeePhone : 9030344566,  
  116.     EmployeeAge : 23,  
  117.     Salary : 20000,  
  118.     City : 'Bangalore',  
  119.     CompanyName : 'TCS'  
  120.   },  
  121.   {  
  122.     EmployeeId : 1045896,  
  123.     EmployeeName : 'Vivek Sharma',  
  124.     EmployeeDesignation : 'Delivery Manager',  
  125.     DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),  
  126.     EmployeeAddress : 'Madhapur, Hyderabad',  
  127.     EmployeePhone : 9030344566,  
  128.     EmployeeAge : 23,  
  129.     Salary : 20000,  
  130.     City : 'Hyderabad',  
  131.     CompanyName : 'Microsoft'  
  132.   }  
  133. ];  
  134. }  
Now run the application by using ng serve command and navigate to http://localhost:4200/ to see the output. We get the same output by using external files or by using inline code, whenever executed the code using inline by keeping all HTML, CSS, Typescript code in app.component.ts file. The below is the ouput with two different scenarios.
 
Learn Components In Angular For Beginners
 

Conclusion

 
In this article, we learned the Components in Angular by using inline code with the metadata, such as selector, template & styles & as well as working with the external files with the metadata, such as selector, templateUrl & styleUrls, etc. We get the same output with two different scenarios.
 
In the next article, we will learn how to navigate between the multiple components. 
 
Thanks, I hope this article helps you.