• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » Javascript » Tutorial Call Parent Function From Child In Angular 4

Tutorial Call Parent Function From Child In Angular 4

By Sigit Prasetya Nugroho ∙ November 11, 2017 ∙ Javascript ∙ 3 Comments

Share : TwitterFacebookTelegramWhatsapp

There are several ways to call the function() in parent component from the child component of angular 4 such as input bindings, setters, service, etc. According to my understanding, the easiest way is using the service. In this tutorial, I will share how to communicate parent and child component using router-outlets and services method.

Another article: Simple Login Form Angular 4 Tutorial For Beginner

As written in the official documentation angular 4, there are seven ways to communicate with parent and child, among others:

  1. Pass data from parent to child with input binding.
  2. Intercept input property changes with a setter.
  3. Intercept input property changes with ngOnChanges().
  4. Parent listens for child event.
  5. Parent interacts with child via local variable.
  6. Parent calls an @ViewChild().
  7. Parent and children communicate via a service.

To be able to communicate via service, we need an angular 4 service that can be executed on each component. We start the following tutorial

Call Parent Function From Child Angular 4 Tutorial

Table of Contents

  • 1 Tutorial angular 4 call parent function from child via service and router-outlet
    • 1.1 Hopefully, tutorial angular 4, How to call parent function from child component can be useful for your application.

Tutorial angular 4 call parent function from child via service and router-outlet

Note: I use angular-cli to create an example of this project

Related Articles :

  • Trick To Redirect New Window Or Tab With Post Method On Angular 5
  • Tutorial To Change Date Format Ng-bootstrap Datepicker Angular 5
  • Tutorial Simple CRUD Angular 5 And Lumen 5.6 For Beginners

1. Create an angular 4 service with the following command

1
ng g service service/com-parent-child

2. Add the following script to com-parent-child.service.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';
 
@Injectable()
export class ComParentChildService {
 
private subjects: Subject[] = [];
 
publish(eventName: string) {
// ensure a subject for the event name exists
this.subjects[eventName] = this.subjects[eventName] || new Subject();
 
// publish event
this.subjects[eventName].next();
}
 
on(eventName: string): Observable {
// ensure a subject for the event name exists
this.subjects[eventName] = this.subjects[eventName] || new Subject();
 
// return observable
return this.subjects[eventName].asObservable();
}
 
}

3. Import service and add provider in app.module.ts

1
2
3
4
5
6
7
8
import { ComParentChildService } from './service/com-parent-child.service';
 
@NgModule({
declarations: [],
imports: [],
providers: [ComParentChildService],
bootstrap: [AppComponent]
})

Examples of using the service are as follows.

1. add the following script to the parent component

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { ComParentChildService} from 'services/com-parent-child.service';
...............
..............
constructor(
private comparentchildservice: ComParentChildService) { }
ngOnInit() {
this.subscription = this.comparentchildservice.on('call-parent').subscribe(() => this.parentFunction());
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
parentFunction(){
console.log('parent function was called')
}

2. Add the following script to the child component

1
2
3
4
5
6
7
8
9
import { ComParentChildService} from 'services/com-parent-child.service';
...............
..............
constructor(
private comparentchildservice: ComParentChildService) { }
 
tesClick(){
this.comparentchildservice.publish('call-parent');
}

3. Add the following script to the child template

1
<button (click)="tesClick()">Test parent function button</button>

If the above script executed, the application will work like the following picture.

Call Parent Function From Child Angular 4 Tutorial

Related article: Tutorial Create Angular 4 Currency Format Directive For Textbox

Reference:

  • Angular 4 documentation.
  • Jasonwatmore.com tutorial site.

Hopefully, tutorial angular 4, How to call parent function from child component can be useful for your application.

Another Javascript Related Post :

  • Adept Using Datatables Plugin In 10 Minutes For Beginners (Tutorial)
  • The Using Of Reactstrap And React-Table, Suitable For Beginners
  • Tutorial Create Simple Block UI Using React JS
  • How To Implement Login Page And Protected Route ReactJS
  • Complete Tutorial React Router Dom For Beginners (Easy-To-Understand)
  • The Right Way Understanding React Lifecycle For Beginners

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for LalitLalit says

    November 29, 2018 at 12:50 pm

    Generic type ‘Observable’ requires 1 type argument(s).

    I’m getting this error

    Reply
  2. Avatar for TestTest says

    February 20, 2019 at 10:16 am

    use any
    Observable

    Reply
  3. Avatar for pulikkanpulikkan says

    August 2, 2019 at 3:44 pm

    Nice article. Very helpful. Could you pls let us know how we can pass an argument to parent function.

    Reply

Leave a Reply Cancel reply

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

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for pulikkanThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (7) Publisher Tips (5) react (3) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) widget (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com