- app3.component.html
- app3.component.spec.ts
- app3.component.css
- app3.component.ts
To understand this article I have created a sample project with three different components and one main component, which is nthing but AppComponent. Now open the app.component.ts and place the below code.
- import { Component } from '@angular/core';
-
- @Component({
- selector: '[app-root]',
-
-
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
-
- EmployeeId = 1702356;
- FirstName = 'Khaja';
- LastName = 'Moizuddin';
- EmployeeName: string = this.FirstName + this.LastName;
- EmployeeDesignation = 'Software Engineer';
- DateOfJoining: Date = new Date('Mon Apr 22 2016 07:44:57');
- EmployeeAddress = 'Malakpet, Hyderabad';
- EmployeePhone = 8019804474;
- EmployeeAge = 29;
- Salary = 20000;
- City = 'Hyderabad';
- CompanyName = 'Legato Health Technologies';
- }
Open the app.component.html and place the below code:
- <h2> Below Details is Displayed From - AppComponent</h2>
- <hr />
-
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
- <tr>
- <td>{{this.EmployeeId}} </td>
- <td>{{this.EmployeeName}}</td>
- <td>{{this.EmployeeDesignation}}</td>
- <td>{{this.DateOfJoining | date:'longDate'}}</td>
- <td>{{this.EmployeeAddress}}</td>
- <td>{{this.EmployeePhone}}</td>
- <td>{{this.EmployeeAge}}</td>
- <td>{{this.Salary | currency: 'USD': true}}</td>
- <td>{{this.City}}</td>
- <td>{{this.CompanyName}}</td>
- </tr>
- </table>
- <app-app1></app-app1>
- <!--<div class='app-app1'></div>
- <div app-app1></div>-->
Open the app.component.css and place the below code:
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 3px solid #c220eb;
- text-align: left;
- padding: 3px;
- }
Similarly open the app1.component.ts file and place the below code:
- import { Component, OnInit } from '@angular/core';
-
- @Component({
- selector: 'app-app1',
-
-
- templateUrl: './app1.component.html',
- styleUrls: ['./app1.component.css']
- })
- export class App1Component implements OnInit {
- employeeList: any[] = [
- {
- EmployeeId : 103621,
- EmployeeName : 'Khaja Moizuddin',
- EmployeeDesignation : 'Software Engineer',
- DateOfJoining : new Date('Mon Apr 22 2016 10:30:57'),
- EmployeeAddress : 'Gacchibowli, Hyderabad',
- EmployeePhone : 8019804474,
- EmployeeAge : 29,
- Salary : 10000,
- City : 'Hyderabad',
- CompanyName : 'Legato'
- },
- {
- EmployeeId : 103622,
- EmployeeName : 'Pavan Kumar',
- EmployeeDesignation : 'S.Software Engineer',
- DateOfJoining : new Date('Tue Jul 19 2012 09:27:45'),
- EmployeeAddress : 'DLF City, Bangalore',
- EmployeePhone : 9030344566,
- EmployeeAge : 25,
- Salary : 15000,
- City : 'Bangalore',
- CompanyName : 'Infosys'
- }
- ];
- constructor() { }
-
- ngOnInit() {
- }
-
- }
Open the app1.component.html and place the below code:
- <h2>Below Details is Displayed From - App1Component</h2>
- <hr>
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
-
- <tr *ngFor = 'let employee of employeeList'>
- <td>{{employee.EmployeeId}} </td>
- <td>{{employee.EmployeeName}}</td>
- <td>{{employee.EmployeeDesignation}}</td>
- <td>{{employee.DateOfJoining | date:'longDate'}}</td>
- <td>{{employee.EmployeeAddress}}</td>
- <td>{{employee.EmployeePhone}}</td>
- <td>{{employee.EmployeeAge}}</td>
- <td>{{employee.Salary | currency: 'USD': true}}</td>
- <td>{{employee.City}}</td>
- <td>{{employee.CompanyName}}</td>
- </tr>
- </table>
-
- <app-app2></app-app2>
- <!--<div class='app-app2'></div>-->
- <!--<div app-app2></div>-->
Open the app1.component.css and place the below code:
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 3px solid #2442ec;
- text-align: left;
- padding: 3px;
- }
-
Similarly open the app2.component.ts and place the below code:
- import { Component, OnInit } from '@angular/core';
-
- @Component({
- selector: 'app-app2',
- templateUrl: './app2.component.html',
- styleUrls: ['./app2.component.css']
- })
- export class App2Component implements OnInit {
- employeeList: any[] = [
- {
- EmployeeId : 165893,
- EmployeeName : 'Vikas',
- EmployeeDesignation : 'S.Software Engineer',
- DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),
- EmployeeAddress : 'Electronic City, Bangalore',
- EmployeePhone : 9030344566,
- EmployeeAge : 23,
- Salary : 20000,
- City : 'Bangalore',
- CompanyName : 'TCS'
- },
- {
- EmployeeId : 1045896,
- EmployeeName : 'Vivek',
- EmployeeDesignation : 'Delivery Manager',
- DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),
- EmployeeAddress : 'Madhapur, Hyderabad',
- EmployeePhone : 9030344566,
- EmployeeAge : 23,
- Salary : 20000,
- City : 'Hyderabad',
- CompanyName : 'Microsoft'
- }
- ];
- constructor() { }
-
- ngOnInit() {
- }
-
- }
Open the app2.component.html and place the below code:
- <h2>Below Details is Displayed From - App2Component</h2>
- <hr>
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
-
- <tr *ngFor = 'let employee of employeeList'>
- <td>{{employee.EmployeeId}} </td>
- <td>{{employee.EmployeeName}}</td>
- <td>{{employee.EmployeeDesignation}}</td>
- <td>{{employee.DateOfJoining | date:'longDate'}}</td>
- <td>{{employee.EmployeeAddress}}</td>
- <td>{{employee.EmployeePhone}}</td>
- <td>{{employee.EmployeeAge}}</td>
- <td>{{employee.Salary | currency: 'INR': true}}</td>
- <td>{{employee.City}}</td>
- <td>{{employee.CompanyName}}</td>
- </tr>
- </table>
- <app-app3></app-app3>
- <!--<div class='app-app3'></div>
- <div app-app3></div>-->
Open the app2.component.css and place the below code:
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 3px solid #056659ce;
- text-align: left;
- padding: 3px;
- }
-
Similarly open the app3.component.ts and place the below code:
- import { Component, OnInit } from '@angular/core';
-
- @Component({
- selector: 'app-app3',
- templateUrl: './app3.component.html',
- styleUrls: ['./app3.component.css']
- })
- export class App3Component implements OnInit {
-
- employeeList: any[] = [
- {
- EmployeeId : 165846,
- EmployeeName : 'Ramesh',
- EmployeeDesignation : 'S.Software Engineer',
- DateOfJoining : new Date('Wed Jul 20 2017 09:24:23'),
- EmployeeAddress : 'Banjara Hills, Bangalore',
- EmployeePhone : 8097984465,
- EmployeeAge : 27,
- Salary : 25000,
- City : 'Hyderabad',
- CompanyName : 'Accenture'
- },
- {
- EmployeeId : 1087564,
- EmployeeName : 'Ashish',
- EmployeeDesignation : 'Delivery Manager',
- DateOfJoining : new Date('Wed Aug 10 2012 11:02:23'),
- EmployeeAddress : 'Mehdipatnam, Hyderabad',
- EmployeePhone : 9030344566,
- EmployeeAge : 26,
- Salary : 20000,
- City : 'Hyderabad',
- CompanyName : 'Microsoft'
- }];
-
- constructor() { }
-
- ngOnInit() {
- }
-
- }
Open the app3.component.html and place the below code:
- <h2>Below Details is Displayed From - App3Component</h2>
- <hr>
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
-
- <tr *ngFor = 'let employee of employeeList'>
- <td>{{employee.EmployeeId}} </td>
- <td>{{employee.EmployeeName}}</td>
- <td>{{employee.EmployeeDesignation}}</td>
- <td>{{employee.DateOfJoining | date:'longDate'}}</td>
- <td>{{employee.EmployeeAddress}}</td>
- <td>{{employee.EmployeePhone}}</td>
- <td>{{employee.EmployeeAge}}</td>
- <td>{{employee.Salary | currency:'EUR':true}}</td>
- <td>{{employee.City}}</td>
- <td>{{employee.CompanyName}}</td>
- </tr>
- </table>
Open the app3.component.css and place the below code:
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 3px solid #eb3737;
- text-align: left;
- padding: 3px;
- }
-
Here in app.component.css, app1.component.css, app2.component.css and app3.component.css, we placed the css with four different colors in order to understand the rendering of HTML table or navigation among four different components.
In order to navigate from one component to another we use the selector with three different types like below.
- Element Declaration using <app-root></app-root>
- Class Declaration using <div class='app-root'></div>
- Attribute Declaration using <div app-root></div>
If we are navigating from App.component to App1.component then in App.component.html we should use the selector of App1.component.ts like below.
- If we want to use as Element Declaration, then in App.component.html, we need to declare it as <app-app1></app-app1> and in the App1.component.ts, the selector should be, selector: 'app-app1'.
- If we want to use as Class Declaration, then in App.component.html, we need to declare it as <div class='app-app1'></div> and in the App1.component.ts, the selector should be, selector: 'app-app1'.
- Similarly if we want to use as Attribute Declaration, then in App.component.html, we need to declare it as <div app-app1></div> and in the App1.component.ts, the selector should be, selector:'[app-app1]'.
In order to work with inline code, we need to place app.component.html, app.component.css code in app.component.ts code and comment the code in app.component.html and app.component.css files to check whether it's working perfectly or not.
Open the app.component.ts file and place the below code:
- import { Component } from '@angular/core';
-
- @Component({
- selector: '[app-root]',
-
-
-
- template:`
- <h2> Below Details is Displayed From - AppComponent</h2>
- <hr />
-
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
- <tr>
- <td>{{this.EmployeeId}} </td>
- <td>{{this.EmployeeName}}</td>
- <td>{{this.EmployeeDesignation}}</td>
- <td>{{this.DateOfJoining | date:'longDate'}}</td>
- <td>{{this.EmployeeAddress}}</td>
- <td>{{this.EmployeePhone}}</td>
- <td>{{this.EmployeeAge}}</td>
- <td>{{this.Salary | currency: 'USD': true}}</td>
- <td>{{this.City}}</td>
- <td>{{this.CompanyName}}</td>
- </tr>
- </table>
- <app-app1></app-app1>
- <!--<div class='app-app1'></div>
- <div app-app1></div>-->
- `,
- styles: ['table { font-family: arial, sans-serif;border-collapse: collapse;width: 100%;} td, th {border: 3px solid #c220eb;text-align: left;padding: 3px;}]']
-
-
-
- })
- export class AppComponent {
-
- EmployeeId = 1702356;
- FirstName = 'Khaja';
- LastName = 'Moizuddin';
- EmployeeName: string = this.FirstName + this.LastName;
- EmployeeDesignation = 'Software Engineer';
- DateOfJoining: Date = new Date('Mon Apr 22 2016 07:44:57');
- EmployeeAddress = 'Malakpet, Hyderabad';
- EmployeePhone = 8019804474;
- EmployeeAge = 29;
- Salary = 20000;
- City = 'Hyderabad';
- CompanyName = 'Legato Health Technologies';
- }
Open the app1.component.ts and place the below code:
- import { Component, OnInit } from '@angular/core';
-
- @Component({
- selector: 'app-app1',
-
-
-
- template: `
- <h2>Below Details is Displayed From - App1Component</h2>
- <hr>
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
-
- <tr *ngFor = 'let employee of employeeList'>
- <td>{{employee.EmployeeId}} </td>
- <td>{{employee.EmployeeName}}</td>
- <td>{{employee.EmployeeDesignation}}</td>
- <td>{{employee.DateOfJoining | date:'longDate'}}</td>
- <td>{{employee.EmployeeAddress}}</td>
- <td>{{employee.EmployeePhone}}</td>
- <td>{{employee.EmployeeAge}}</td>
- <td>{{employee.Salary | currency: 'USD': true}}</td>
- <td>{{employee.City}}</td>
- <td>{{employee.CompanyName}}</td>
- </tr>
- </table>
-
- <app-app2></app-app2>
- <!--<div class='app-app2'></div>-->
- <!--<div app-app2></div>-->
-
- `,
- styles:['table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th { border: 3px solid #2442ec;text-align: left;padding: 3px;}']
-
- })
- export class App1Component implements OnInit {
- employeeList: any[] = [
- {
- EmployeeId : 103621,
- EmployeeName : 'Khaja Moizuddin',
- EmployeeDesignation : 'Software Engineer',
- DateOfJoining : new Date('Mon Apr 22 2016 10:30:57'),
- EmployeeAddress : 'Gacchibowli, Hyderabad',
- EmployeePhone : 8019804474,
- EmployeeAge : 29,
- Salary : 10000,
- City : 'Hyderabad',
- CompanyName : 'Legato'
- },
- {
- EmployeeId : 103622,
- EmployeeName : 'Pavan Kumar',
- EmployeeDesignation : 'S.Software Engineer',
- DateOfJoining : new Date('Tue Jul 19 2012 09:27:45'),
- EmployeeAddress : 'DLF City, Bangalore',
- EmployeePhone : 9030344566,
- EmployeeAge : 25,
- Salary : 15000,
- City : 'Bangalore',
- CompanyName : 'Infosys'
- }
- ];
- constructor() { }
-
- ngOnInit() {
- }
-
- }
Open the app2.component.ts and place the below code:
- import { Component, OnInit } from '@angular/core';
-
- @Component({
- selector: 'app-app2',
- template: `
- <h2>Below Details is Displayed From - App2Component</h2>
- <hr>
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
-
- <tr *ngFor = 'let employee of employeeList'>
- <td>{{employee.EmployeeId}} </td>
- <td>{{employee.EmployeeName}}</td>
- <td>{{employee.EmployeeDesignation}}</td>
- <td>{{employee.DateOfJoining | date:'longDate'}}</td>
- <td>{{employee.EmployeeAddress}}</td>
- <td>{{employee.EmployeePhone}}</td>
- <td>{{employee.EmployeeAge}}</td>
- <td>{{employee.Salary | currency: 'INR': true}}</td>
- <td>{{employee.City}}</td>
- <td>{{employee.CompanyName}}</td>
- </tr>
- </table>
- <app-app3></app-app3>
- <!--<div class='app-app3'></div>
- <div app-app3></div>-->
-
- `,
- styles:['table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 3px solid #056659ce;text-align: left;padding: 3px;}']
-
-
- })
- export class App2Component implements OnInit {
- employeeList: any[] = [
- {
- EmployeeId : 165893,
- EmployeeName : 'Vikas',
- EmployeeDesignation : 'S.Software Engineer',
- DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),
- EmployeeAddress : 'Electronic City, Bangalore',
- EmployeePhone : 9030344566,
- EmployeeAge : 23,
- Salary : 20000,
- City : 'Bangalore',
- CompanyName : 'TCS'
- },
- {
- EmployeeId : 1045896,
- EmployeeName : 'Vivek',
- EmployeeDesignation : 'Delivery Manager',
- DateOfJoining : new Date('Wed Mar 02 2015 08:48:23'),
- EmployeeAddress : 'Madhapur, Hyderabad',
- EmployeePhone : 9030344566,
- EmployeeAge : 23,
- Salary : 20000,
- City : 'Hyderabad',
- CompanyName : 'Microsoft'
- }
- ];
- constructor() { }
-
- ngOnInit() {
- }
-
- }
And finally open the app3.component.ts and place the below code:
- import { Component, OnInit } from '@angular/core';
-
- @Component({
- selector: 'app-app3',
- template: `
- <h2>Below Details is Displayed From - App3Component</h2>
- <hr>
- <table>
- <tr>
- <th>EmployeeId</th>
- <th>EmployeeName</th>
- <th>EmployeeDesignation</th>
- <th>DateOfJoining</th>
- <th>EmployeeAddress</th>
- <th>EmployeePhone</th>
- <th>EmployeeAge</th>
- <th>Salary</th>
- <th>City</th>
- <th>CompanyName</th>
- </tr>
-
-
- <tr *ngFor = 'let employee of employeeList'>
- <td>{{employee.EmployeeId}} </td>
- <td>{{employee.EmployeeName}}</td>
- <td>{{employee.EmployeeDesignation}}</td>
- <td>{{employee.DateOfJoining | date:'longDate'}}</td>
- <td>{{employee.EmployeeAddress}}</td>
- <td>{{employee.EmployeePhone}}</td>
- <td>{{employee.EmployeeAge}}</td>
- <td>{{employee.Salary | currency:'EUR':true}}</td>
- <td>{{employee.City}}</td>
- <td>{{employee.CompanyName}}</td>
- </tr>
- </table>
-
- `,
- styles:['table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 3px solid #eb3737;text-align: left;padding: 3px;}']
-
-
- })
- export class App3Component implements OnInit {
-
- employeeList: any[] = [
- {
- EmployeeId : 165846,
- EmployeeName : 'Ramesh',
- EmployeeDesignation : 'S.Software Engineer',
- DateOfJoining : new Date('Wed Jul 20 2017 09:24:23'),
- EmployeeAddress : 'Banjara Hills, Bangalore',
- EmployeePhone : 8097984465,
- EmployeeAge : 27,
- Salary : 25000,
- City : 'Hyderabad',
- CompanyName : 'Accenture'
- },
- {
- EmployeeId : 1087564,
- EmployeeName : 'Ashish',
- EmployeeDesignation : 'Delivery Manager',
- DateOfJoining : new Date('Wed Aug 10 2012 11:02:23'),
- EmployeeAddress : 'Mehdipatnam, Hyderabad',
- EmployeePhone : 9030344566,
- EmployeeAge : 26,
- Salary : 20000,
- City : 'Hyderabad',
- CompanyName : 'Microsoft'
- }];
-
- constructor() { }
-
- ngOnInit() {
- }
-
- }
In order to check the Output, navigate to the path where the project is created, like, F:\NavigationBetweenComponents\NavigationBetweenComponents and execute
ng serve command. And now open the browser of your choice and navigate to http://localhost:4200/. We will get the same output for inline code as well as working with external component files.
The output is shown below.
Conclusion
In this article, we learned how to navigate from one component to another component by using three different declarations; i.e., Element Declaration, Class Declaration & Attribute Declaration.
In my next article, I will be working on different pipes which are available in Angular.
Thanks & I hope this article helps you.