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

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

2 months ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

3 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

3 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

3 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

3 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

3 months ago