- Angular 2 - Services
- Angular 2 - Nested Containers
- Angular 2 - Lifecycle Hooks
- Angular 2 - User Input
- Angular 2 - Custom Pipes
- Angular 2 - Transforming Data
- Angular 2 - Handling Events
- Angular 2 - Data Display
- Angular 2 - Third Party Controls
- Angular 2 - Advanced Configuration
- Angular 2 - Dependency Injection
- Angular 2 - CLI
- Angular 2 - Forms
- Angular 2 - Navigation
- Angular 2 - Routing
- Angular 2 - Error Handling
- CRUD Operations Using HTTP
- Angular 2 - Data Binding
- Angular 2 - Metadata
- Angular 2 - Directives
- Angular 2 - Templates
- Angular 2 - Components
- Angular 2 - Architecture
- Angular 2 - Modules
- Angular 2 - Hello World
- Angular 2 - Environment
- Angular 2 - Overview
- Angular 2 - Home
Angular 2 Useful Resources
- Angular 2 - Discussion
- Angular 2 - Useful Resources
- Angular 2 - Quick Guide
- Angular 2 - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Angular 2 - Nested Containers
In Angular JS, it is possible to nest containers inside each other. The outside container is known as the parent container and the inner one is known as the child container. Let’s look at an example on how to achieve this. Following are the steps.
Step 1 − Create a ts file for the child container called child.component.ts.
Step 2 − In the file created in the above step, place the following code.
import { Component } from @angular/core ; @Component ({ selector: child-app , template: <span> {{values}} </span> }) export class ChildComponent { values = ; ngOnInit() { this.values = "Hello"; } }
The above code sets the value of the parameter this.values to “Hello”.
Step 3 − In the app.component.ts file, place the following code.
import { Component } from @angular/core ; import { ChildComponent } from ./child.component ; @Component ({ selector: my-app , template: <child-app></child-app> }) export class AppComponent { }
In the above code, notice that we are now calpng the import statement to import the child.component module. Also we are calpng the <child-app> selector from the child component to our main component.
Step 4 − Next, we need to ensure the child component is also included in the app.module.ts file.
import { NgModule } from @angular/core ; import { BrowserModule } from @angular/platform-browser ; import { AppComponent } from ./app.component ; import { MultipperPipe } from ./multipper.pipe import { ChildComponent } from ./child.component ; @NgModule ({ imports: [BrowserModule], declarations: [AppComponent, MultipperPipe, ChildComponent], bootstrap: [AppComponent] }) export class AppModule {}
Once you save all the code changes and refresh the browser, you will get the following output.
Advertisements