Parent Child Component Sharing Data in Angular 6

Parent Child Component Sharing Data in Angular 6

Parent to Child: Sharing Data via Input

It works by using the @Input() decorator to allow data to be passed via the template. Create the child component.

Another way to share data is to emit data from the child, which can be listed to by the parent. This approach is ideal when you want to share data changes that occur on things like button clicks, and other user events.

ng g component movieinfo

app.component.ts

import { Component } from '@angular/core';
import { Subject } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  message: string;
  id: number; 
  name?: string; 
  movies: any[] = []; 
  selectedMovie: any[];

  constructor() { 
   this.movies = [
     {
        id:1,
        name: "Avatar",
        year: 2014,
        intro: "Avatar, marketed as James Cameron's Avatar, is a 2009 American epic science fiction film directed, written, produced, and co-edited by James Cameron"
     },
     {
       id:2,
       name: "Titanic",
       year: 1998,
       intro: "Titanic is a 1997 American epic romance-disaster film directed, written, co-produced and co-edited by James Cameron"
     },
     {
        id:3,
        name: "Patriot",
        year: 2000,
        intro: "The Patriot is a 2000 American epic historical fiction war film directed by Roland Emmerich, written by Robert Rodat"
     }
   ];
   this.selectedMovie = this.movies[0];
  }

  getselect(list){
    this.selectedMovie = list;
  }

  recievemsg($event){
    this.message = $event;
    console.log("Msg send by child to parent : "+$event);
  }


}

app.component.html

this.selectedMovie = this.movies[0] is used for selecting first link of left panel on load. selectedMovie variable is used to store value which i have selected onclick. on click a function is called "getselect(list)" and passing the selected list value as a parameter which value is stored in this.selectedMovie. Now check using "list === selectedMovie", if true then "selected" class will be added in a tag using [class.selected]. Now i am sending "selectedMovie" value from parent component to child movieinfo component using [list]="slectedMovie".

<div style="text-align:center"><h1>
    Welcome to {{ title }}!
  </h1>
</div>


<div class="col-md-3"><div class="text-right">{{message}}</div><ul class="links"><li *ngFor="let list of movies"><a (click)="getselect(list)" [class.selected]="list === selectedMovie" href="javascript:void(0)"><span>{{list.id}}.</span><span>{{list.name}}</span>        
        </a>  
      </li></ul>
</div>

<div class="col-md-9"><app-movieinfo  [list]="selectedMovie" (MessageEvent)="recievemsg($event)"></app-movieinfo>
</div>

app.component.css

.links{ list-style: none;}
.links a{
    background: #EEE;
    padding: 10px 20px;
    display: block;
    margin-bottom: 5px;
}
.links a.selected{
    background: #444;
    color: #FFF;
}

movieinfo.component.ts

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Subject } from 'rxjs';

@Component({
  selector: 'app-movieinfo',
  templateUrl: './movieinfo.component.html',
  styleUrls: ['./movieinfo.component.css']
})
export class MovieinfoComponent implements OnInit {

  constructor() { }

  @Input() list : Subject<any>;

  @Output() MessageEvent = new EventEmitter();

  ngOnInit() {
  }

  sendmsg(name){
    this.MessageEvent.emit("Selected Movie "+name);
  }

}

movieinfo.component.html

<div *ngIf="list"><h2>{{list.id}}. {{list.name | uppercase}}</h2><h4>Year : {{list.year}}</h4><h4>Intro : {{list.intro}}</h4>

  <button class="btn btn=primary" (click)="sendmsg(list.name)">Send Message to Parent</button>
</div>

we create a private Behavior Subject that will hold the list value of the message. Subject is used for sharing data. You have to declare this list variable using @Input decorator.

we create a private Behavior Subject that will hold the list value of the message. Subject is used for sharing data. You have to declare this list variable using @Input decorator.

In the child, we declare a messageEvent variable with the Output decorator and set it equal to a new event emitter. Then we create a function named sendmsg(list.name) that calls emit on this event with the message we want to send. Lastly, we create a button to trigger this function.

The parent can now subscribe to this messageEvent that’s outputted by the child component, then run the receive message function whenever this event occurs.

Download Code : https://github.com/piyalidas10/Sharing-Data-Parent-Child-Component

Great

回复

Nice Article. If you add some more to it then that may complete the answer. When you sending data from child to parent at that time your calling/emitting the functionality in?app-movieinfo component. Assume I want to call/emit from any other component then? Example:? My Parent component is app Child one component is child1 Child two component is child2 child1 and child2 both are used in app component Now I created @Output() variable in child2 and calling/emitting? & displaying data in child1

要查看或添加评论,请登录

Piyali Das的更多文章

  • Accessible Bootstrap Dropdown Navigation

    Accessible Bootstrap Dropdown Navigation

    The Focus Focus refers to what element in your application (such as a field, checkbox, button, or link) currently…

  • Front-end development using Gulp

    Front-end development using Gulp

    Gulp is a JavaScript toolkit which helps you in implementing multiple front end tasks during web development, it can be…

  • Angular Dynamic Context Menu

    Angular Dynamic Context Menu

    "contextmenu" is an event like click, mouseup, mousemove, mousedown. This "contextmenu" event is typically triggered by…

    2 条评论
  • Right-click Context Menu In JavaScript

    Right-click Context Menu In JavaScript

    "contextmenu" is an event like click, mouseup, mousemove, mousedown. This "contextmenu" event is typically triggered by…

  • Dynamic Height in Angular Application

    Dynamic Height in Angular Application

    In this tutorial, i have explained how you an dynamically set height of a div of component depending on other div of…

  • Code Optimization Advantage with Example

    Code Optimization Advantage with Example

    Dynamic Font Change non-optimize code HTML code…

  • Advantages of Angular Library in Architectural Design

    Advantages of Angular Library in Architectural Design

    Application Strategic Design Decompose the big application into smaller libraries Smaller libraries are easily…

  • Angular Multi Application Project Creation

    Angular Multi Application Project Creation

    If your installed Angular global version is different and you want to create different version Angular application…

    1 条评论
  • Tree shaking vs. Non tree shaking providers in Angular

    Tree shaking vs. Non tree shaking providers in Angular

    In our example we are importing and referencing our Service in the AppModule causing an explicit dependency that cannot…

  • Angular Port Change

    Angular Port Change

    3 steps to change port in Angular Change in Angular.json Change in script of package.

    2 条评论

社区洞察

其他会员也浏览了