Angular 2 – Parent-Child Components Explained with Code Examples

This article represents concepts and code examples for creating parent-child components. Following image represents the view which is referred in the code samples below.
Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following is explained later in this blog.

  • A parent component, HelloWorld and the interaction with Child Component, UserComponent
  • A child component, UserComponent
  • An interface User

 

Parent Component, HelloWorld, interaction with Child Component, UserComponent

A parent component, HelloWorld, represents the view consisting of Hello message and a textfield for user to interact with the view. The Hello message looks like <h1>Hello, <user [value]=”user”></user>&t;/h1>. Notice that the user object is passed to the child component, User. Following code represents the HelloWorld. Notice the following:

  • Structural components such as Component, View are imported.
  • User Interface is imported. User interface is primarily a contract for creating a User object. The User interface is used both in parent and child components. Following is the code for interface:
    export interface User {
    id: number,
    name: string,
    email: string,
    address: string
    }
    
  • Child component UserComponent is imported. Check below the code for UserComponent.
  • Child component user binds property “value” with user object. Note the code, [value]=”user”.
  • The object is type user is created as part of HelloWorld component. Check the code such as following:
    public user: User = {
    id: 1,
    name: 'Calvin Hobbes',
    email: 'calvin.hobbes@gmail.com',
    address: 'California, USA'
    };
    
  • The UserComponent is defined as part of “directives” property of Component decorator definition.
import {Component, View} from 'angular2/core';
import {User} from './user';
import {UserComponent} from './user.component';
import {bootstrap} from 'angular2/platform/browser';

@Component({
selector: 'hello-text'
})
@View({
template: `<h1>Hello, <user [value]="user"></user></h1>
<hr/>
<div>
<form>
<div class="form-group">
<label for="exampleInputEmail1">Your Name</label>
<input type="text" class="form-control" [(ngModel)]="user.name" placeholder="Enter your name...">
</div>
</form>
</div>
`,
directives: [UserComponent]
})
export class HelloWorld {
public user: User = {
id: 1,
name: 'Calvin Hobbes',
email: 'calvin.hobbes@gmail.com',
address: 'California, USA'
};
};
bootstrap(HelloWorld);

 

Child Component, UserComponent

Pay attention to following in the code given below:

  • User interface is imported
  • The component property, value, is specified of the type, User.
  • The Component property “inputs” is used to list the property which is used to receive the value from parent. In this case, it is “value” property.
import {Component, View} from 'angular2/core';
import {User} from './user';

@Component({
selector: 'user',
inputs: ['value']
})
@View({
template: '{{value.name}}'
})
export class UserComponent {
value: User;
}

 

Interface, User

Note the following in the code given below:

  • “export” keyword is used to allow modules to use User interface.
  • Interface defines properties of the User object.
export interface User {
id: number,
name: string,
email: string,
address: string
}

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar

Recent Posts

Mathematics Topics for Machine Learning Beginners

In this blog, you would get to know the essential mathematical topics you need to…

6 days ago

Questions to Ask When Thinking Like a Product Leader

This blog represents a list of questions you can ask when thinking like a product…

1 week ago

Three Approaches to Creating AI Agents: Code Examples

AI agents are autonomous systems combining three core components: a reasoning engine (powered by LLM),…

2 weeks ago

What is Embodied AI? Explained with Examples

Artificial Intelligence (AI) has evolved significantly, from its early days of symbolic reasoning to the…

2 months ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

5 months ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

5 months ago