简体   繁体   English

如何在Angular 4中将值从子组件传递给父组件

[英]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. 我正在使用Angular4应用程序,我正在尝试将值从子组件传递到父组件。我已经实现了一些东西,但我无法按预期获得输出。

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... 子组件.ts代码......

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... 父组件.ts代码......

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 ..? 在Angular 4中是否有可能将数据从子节点绑定到父节点?

Thanks... 谢谢...

You are emitting the value by using event emitter sendCount . 您使用事件发射器sendCount发出值。 You have to receive it in parent component by using child component like this: 你必须使用这样的子组件在父组件中接收它:

parent.html parent.html

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

parent.ts parent.ts

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

Also, there are other ways of communicating like subject in rxjs or by using shared service. 此外,还有其他方式可以在rxjs使用subject或使用共享服务进行通信。

In angular 4 you can use @Input, @Output decorators for values or Emit the data. 在角度4中,您可以使用@输入,@输出装饰器作为值或发送数据。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 API 返回数据数组值从父组件传递到 Angular 中的子组件 - How to pass API returning data array values from Parent Component to Child Component in Angular Angular:我可以将值从控制台传递给组件,就像父组件如何将值传递给它的子组件一样? - Angular: Can I pass values from the console to a component like how a parent component passes a value to its child? Angular 2,如何将选项从父组件传递到子组件? - Angular 2, How to pass options from parent component to child component? 如何在 angular 中将对象数组从子组件传递到父组件 - How to pass Array of Objects from Child Component to Parent Component in angular 如何在Angular4中将数据从子组件传递到父组件 - How to pass data from child component to parent component in Angular4 如何以角度将数据从父组件传递到自动完成子组件 - How to Pass data to autocomplete child component from parent component in angular 如何将角度5中的点击数据从父组件传递到子组件? - How to pass data on click in angular 5 from parent component to child component? Angular:如何将数据从父组件传递到子组件? - Angular : How to pass data from parent component to child component? angular中如何将数据从父组件传回子组件 - How to pass back data from parent component to child component in angular 如何从父组件向子组件传递数据 angular 14 - How to pass data from parent component to child component angular 14
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM