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...

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...
The @Output() decorator in a child component or directive lets data flow from the child to the parent. @Output() marks a property in a child component as a doorway through which data can travel from the child to the parent.
To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With