How to emit value from parent to child Angular?

Emitting Value from Parent to Child in Angular

Angular is a powerful JavaScript framework for building web applications. One of the key features of Angular is its ability to emit value from parent to child components. In this article, we will explore how to achieve this in Angular.

Why Emit Value from Parent to Child?

Before we dive into the solution, let’s understand why we need to emit value from parent to child in Angular. Emitting value from parent to child allows us to share data between components, enabling a more modular and maintainable codebase. This is particularly useful when working with complex data models or when we need to reuse code across multiple components.

Basic Concepts

Before we start, let’s cover some basic concepts that we’ll need to understand:

  • Component: A component is the basic building block of an Angular application. It’s a self-contained piece of code that can render a view and handle input/output operations.
  • Template: A template is the HTML code that’s rendered by the component. It’s used to define the structure and layout of the component’s UI.
  • Component Template: The component template is the HTML code that’s used to render the component’s UI. It’s typically defined in a separate file (e.g., app.component.html).
  • Component Class: The component class is the code that’s used to define the component’s behavior. It’s typically defined in a separate file (e.g., app.component.ts).

Emitting Value from Parent to Child

To emit value from parent to child, we need to use the @Input() decorator to bind the parent component’s property to the child component’s property. Here’s an example:

// parent.component.ts
import { Component } from '@angular/core';

@Component({
selector: 'app-parent',
template: `
<h1>Parent Component</h1>
<p>Value from Parent: {{ parentValue }}</p>
`
})
export class ParentComponent {
parentValue = 'Initial Value';

constructor() { }

ngOnInit(): void {
this.parentValue = 'Updated Value';
}
}

// child.component.ts
import { Component } from '@angular/core';

@Component({
selector: 'app-child',
template: `
<h1>Child Component</h1>
<p>Value from Child: {{ childValue }}</p>
`
})
export class ChildComponent {
childValue = 'Initial Value';

constructor() { }

ngOnInit(): void {
this.childValue = 'Updated Value';
}
}

In this example, we’ve defined two components: ParentComponent and ChildComponent. The ParentComponent has a parentValue property that’s bound to the childValue property using the @Input() decorator. The ChildComponent has a childValue property that’s bound to the parentValue property using the @Input() decorator.

Using a Service

Another way to emit value from parent to child is by using a service. A service is a singleton that provides a way to share data between components. Here’s an example:

// parent.service.ts
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class ParentService {
parentValue = 'Initial Value';

get value(): string {
return this.parentValue;
}

set value(value: string) {
this.parentValue = value;
}
}

// child.service.ts
import { Injectable } from '@angular/core';
import { ParentService } from './parent.service';

@Injectable({
providedIn: 'root'
})
export class ChildService {
constructor(private parentService: ParentService) { }

ngOnInit(): void {
this.parentService.value = 'Updated Value';
}
}

In this example, we’ve defined two services: ParentService and ChildService. The ParentService provides a value property that’s bound to the parentValue property using the @Input() decorator. The ChildService uses the ParentService to get and set the value property.

Using a Shared Module

Another way to emit value from parent to child is by using a shared module. A shared module is a separate module that’s shared across multiple components. Here’s an example:

// shared.module.ts
import { NgModule } from '@angular/core';
import { ParentComponent } from './parent.component';
import { ChildComponent } from './child.component';

@NgModule({
declarations: [ParentComponent, ChildComponent],
exports: [ParentComponent, ChildComponent]
})
export class SharedModule {}

// parent.component.ts
import { Component } from '@angular/core';
import { SharedModule } from './shared.module';

@Component({
selector: 'app-parent',
template: `
<h1>Parent Component</h1>
<p>Value from Parent: {{ parentValue }}</p>
`
})
export class ParentComponent {
parentValue = 'Initial Value';

constructor(private sharedModule: SharedModule) { }

ngOnInit(): void {
this.parentValue = 'Updated Value';
}
}

// child.component.ts
import { Component } from '@angular/core';
import { SharedModule } from './shared.module';

@Component({
selector: 'app-child',
template: `
<h1>Child Component</h1>
<p>Value from Child: {{ childValue }}</p>
`
})
export class ChildComponent {
childValue = 'Initial Value';

constructor(private sharedModule: SharedModule) { }

ngOnInit(): void {
this.childValue = 'Updated Value';
}
}

In this example, we’ve defined a shared module that’s shared across multiple components. The ParentComponent and ChildComponent use the SharedModule to get and set the parentValue and childValue properties.

Conclusion

In this article, we’ve explored how to emit value from parent to child in Angular. We’ve covered basic concepts, emitted value from parent to child, using a service, and using a shared module. By following these examples, you can create a more modular and maintainable codebase in your Angular applications.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

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

Scroll to Top