Code Hunter
Code Hunter

Reputation: 153

How to pass values from child to parent component in Angular 4

I am working in an Angular4 application ,In I'm trying to pass values from child component to parent component .I have implemented something but I can't get the output as expected.

Child component image...

Total product count

Child Component Code...

  <div class="row">
                    <div class="col-3">
                        <input type="text" readonly  class="form-control col-8 text-center bg-white font-weight-bold " id="counterval" value="{{counter}}" #Totalcartval>
                    </div>         
                        <a class="btn btn-primary text-white" (click)="decrement()" role="button">
                            <i class="fa fa-minus" style="font-size:15px"></i>
                        </a>
                        <div class="col-1"></div>
                        <a class="btn btn-primary text-white" (click)="increment()"  role="button">
                            <i class="fa fa-plus" style="font-size:15px"></i>
                        </a>            
                </div>

 <button type="button" (click)="sendRecord(Totalcartval.value)" class="btn btn-success" data-toggle="modal" data-target=".bd-example-modal-lg">Add To Cart</button>

Child component .ts code...

import { Component,EventEmitter,Output } from '@angular/core';
import { Router } from '@angular/router';
import { SessionStorageService,SessionStorage } from 'angular-web-storage';


@Component({
  selector: 'app-my-cart',
  templateUrl: './my-cart.component.html',
  styleUrls: ['./my-cart.component.css'],
  outputs :['ChildEvent']
})

export class MyCartComponent  {

  public counter : number = 1;   

    increment(){
      this.counter += 1;
    }

    decrement(){  
      if(this.counter >1)
      {
      this.counter -= 1;
      }
    }
    @Output() sendCount : EventEmitter <number> = new EventEmitter<number>();
    public sendRecord(Totalcartval : number ) {  
      this.sendCount.emit(Totalcartval);
    }

  constructor(private router:Router) {
    this.router.events.subscribe(
      () => window.scrollTo(0, 0)
    );
  }

  ngOnInit() {}

}

I got the textbox values in

this.sendCount.emit(Totalcartval);

I can't able to receive it it parent component and display it in the required field...

Parent Component code..

<form class="form-inline">
        <div class="input-group">
          <div class="input-group-prepend">
            <span class="input-group-text" id="basic-addon1">
                <i class="fas fa-shopping-bag text-primary" style="font-size:40px"></i>
            </span>
          </div>
          <input type="text" class="form-control bg-white"  placeholder="My Cart" value="{{totalcartval}}" readonly >  
        </div>
      </form>

I want to display the data in the above textbox

Parent component .ts code...

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DatePipe } from '@angular/common';
import { HostListener } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit{
    today = Date.now();
    fixedTimezone =this.today;
    res : string;

    public totalcartval :number;
    public getRecord(data: number) : void {
            this.totalcartval = data;
    }

    constructor(private http: HttpClient) {

    }
}

And is there any possibility to bind data from child to parent in Angular 4 ..?

Thanks...

Upvotes: 11

Views: 36231

Answers (2)

kzrfaisal
kzrfaisal

Reputation: 1443

You are emitting the value by using event emitter sendCount. You have to receive it in parent component by using child component like this:

parent.html

<app-my-cart (sendCount)="customFunc($event)"></app-my-cart>

parent.ts

customFunc(data){
    this.totalcartval = data;
}

Also, there are other ways of communicating like subject in rxjs or by using shared service.

Upvotes: 15

Sagar Kharab
Sagar Kharab

Reputation: 369

In angular 4 you can use @Input, @Output decorators for values or Emit the data.

Please refer https://angular.io/guide/component-interaction

Upvotes: 0

Related Questions