Angular2: Parent and Child Components Communication
Asked Answered
C

2

4

I'm trying to create a parent and child component where the child component is going to have a states drop down. Can someone help me understand how I can access the states drop down value in Parent Component? Here is my sample code.

/app/app.ts

import {Component} from 'angular2/core'
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators} from 'angular2/common'
import {State} from './state'

@Component({
  selector: 'my-app',
  providers: [FormBuilder],
  templateUrl: 'app/app.html',
  directives: [State]
})

export class App {
  registrationForm: ControlGroup;

  state: State;

  constructor(fb: FormBuilder) {

    this.registrationForm = fb.group({
      'name': ['', Validators.required],
      'email': ''
    });
  }

  onSubmit() {
    alert('Entered Name: ' + this.registrationForm.value.name);
    alert('State Selected: '); //trying to alert the state selected in state component
  }
}

/app/app.html

<div>
  <h2>Registration Form</h2>
  <form [ngFormModel]=registrationForm (ngSubmit)="onSubmit()">
    <label>Name: </label>
    <input type="text" ngControl="name">
    <state></state>
    <button [disabled]="!registrationForm.valid">Submit</button>
  </form>
</div>

/app/state.ts
import {Component} from 'angular2/core'
import {FORM_DIRECTIVES, FormBuilder, ControlGroup} from 'angular2/common'

@Component({
  selector: 'state',
  providers: [FormBuilder],
  templateUrl: 'app/state.html',
  directives: []
})

export class State {

  statesDropDownValues = ['NJ', 'NY', 'PA', 'CA'];

  stateForm: ControlGroup

  constructor(fb: FormBuilder) {
    this.stateForm = fb.group({
      'state': ''
    });

  }

  setStateValue() {
    alert('State Selected: ' + this.stateForm.value.state);
  }
}

/app/state.html
<div>
  <form [ngFormModel]="stateForm">
    <label>State: </label>
    <select ngControl="state" (change)="setStateValue()">
      <option *ngFor="#s of statesDropDownValues"
          [value]="s">{{s}}
      </option>
    </select>
  </form>
</div>

Also plunker here http://plnkr.co/edit/8tsm9sYeH8d8ulfqQKxY?p=info

Creasy answered 30/3, 2016 at 18:30 Comment(2)
For child -> parent communication see angular.io/docs/ts/latest/cookbook/…Krupp
Perfect. Thanks for sharing this link. I've got an idea of how the parent child communication works.Creasy
N
3

I would define an output into your State component and triggers an event using it:

@Component({
  selector: 'state',
  providers: [FormBuilder],
  templateUrl: 'app/state.html',
  directives: []
})
export class State {
  statesDropDownValues = ['NJ', 'NY', 'PA', 'CA'];
  stateForm: ControlGroup;
  @Output()
  stateChange:EventEmitter<string> = new EventEmitter(); // <----

  constructor(fb: FormBuilder) {
    this.stateForm = fb.group({
      'state': ''
    });
  }

  setStateValue() {
    alert('State Selected: ' + this.stateForm.value.state);
    stateChange.emit(this.stateForm.value.state);
  }
}

The parent component can register on this event to be notified of changes:

<div>
  <h2>Registration Form</h2>
  <form [ngFormModel]=registrationForm (ngSubmit)="onSubmit()">
    <label>Name: </label>
    <input type="text" ngControl="name">
    <state (stateChange)="handleNewState($event)"></state>
    <button [disabled]="!registrationForm.valid">Submit</button>
  </form>
</div>

$event contains the value of the new state value.

Edit

Here is a way to save the selected state in the parent component:

export class App {
  registrationForm: ControlGroup;

  state: string;

  constructor(fb: FormBuilder) {
    this.registrationForm = fb.group({
      'name': ['', Validators.required],
      'email': ''
    });
  }

  handleNewState(state) {
    this.state = state;
  }

  onSubmit() {
    alert('Entered Name: ' + this.registrationForm.value.name);
    alert('State Selected: ' + this.state);
  }
}
Nessie answered 30/3, 2016 at 19:3 Comment(2)
Thank You. That was definitely helpful. But how can I get the value of the state selected in my onSubmit() function?Creasy
You could also use '@ContentChild' or '@ViewChild' to access the properties of childHoneyed
K
0

Using output and emit you can easily access child data in parent component. For example

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

@Component({
  selector: 'child-component',
  template: '<div>'
      +'<span>Child Component: </span>'
      +'<span>{{name}}</span>'
      +'<button (click)="shareData()">Share data to parent</button>'
    +'</div>',
  styleUrls: ['./app.component.css']
})
export class ChildComponent {
 
 @Input() name: string;
 @Output() shareDataToParent = new EventEmitter();
 
 title : string;
 constructor(){
     this.title = 'Sharing from child to parent';
 }
 
 shareData(){
     this.shareDataToParent.emit(this.title);
 }
}

<child-component name="Angular" (shareDataToParent) = shareDataToParent($event)>
</child-component>

Parent component

shareDataToParent(sharedText:string){
  console.log(sharedText);
}
Kola answered 17/10, 2017 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.