Nested Component in Angular2?

Nested Component in Angular2?

Nested component means a component under another component. Just to iterate what I discussed in my early post , component is a class with a template and  decorator/metadata.

However, if you want to know more details about component then please visit my previous post –

Hence, we will create below tables with nested component approach. As you can see we can divide the form in two parts, one is header and another one is table.  First of all we will see how to create table header then its contents, after that we will connect both as nested component.

Student Details

1. Modify app.component.ts code with below code –

import {Component} from “@angular/core”;





export class AppComponent{

pageHeader: string = “Student Details”;





2. Append below code in file app.component.html.





3. Then run the code. The output will be like below screenshot. So, our header is ready.

Student Details 2

4. Now we have to create table and its content. Under ‘App’ folder in the project directory create a folder named ‘Students’ and then under that create two files i.e. student. component.html and student. component.ts



<td>First Name</td>




<td>Last Name</td>













5. Add below code to student.component.ts file –

import {Component} from “@angular/core”


selector :’my-students’,

templateUrl: ‘student.component.html’



export class StudentsComponent {

firstName:string= “krishnan”;






6. Our student component is ready and as well as Header component also. Now a connection need to establish to make it nested.

7. Append selector <my-student></my-student> to  the file app.component.html


8. Modify file ‘app.module.ts’ file with below marked code . This file was created during project creation –

import { BrowserModule } from ‘@angular/platform-browser’;

import { NgModule } from ‘@angular/core’;

import { AppComponent } from ‘./app.component’;

import { StudentsComponent } from ‘./students/student.component’;


declarations: [




imports: [



providers: [],

bootstrap: [AppComponent]


export class AppModule { }



One Reply to “Nested Component in Angular2?”

Leave a Reply

Your email address will not be published. Required fields are marked *