如何将数据从子组件传递到父组件 Angular [英] How to Pass data from child to parent component Angular

查看:29
本文介绍了如何将数据从子组件传递到父组件 Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 search_detail 的组件,其中有另一个名为日历的组件,

SearchDetail_component.html

 
  • <div class="btn-group dropdown" [class.open]="DatedropdownOpened"><button type="button" (click)="DatedropdownOpened = !DatedropdownOpened" class="btn btn-default dropdown-toggle" data-toggle="dropdown"aria-haspopup="true" [attr.aria-expanded]="dropdownOpened ? 'true': 'false'">日期 <span class="caret"></span><ul class="dropdown-menu default-dropdown"><日历></日历><按钮>取消</button><button(点击)="setDate(category)">好的 </button>
  • SearchDetail_component.ts

    import 'rxjs/add/observable/fromEvent';@成分({选择器:'searchDetail',templateUrl: './search_detail.component.html',模块 ID:module.id})

    Calendar.component.ts

    import { Component, Input} from '@angular/core';@成分({moduleId:module.id,选择器:'日历',templateUrl: './calendar.component.html'})导出类 CalendarComponent{public fromDate:Date = new Date();private toDate:Date = new Date();私人事件:Array;明天私人:日期;私人之后明天:日期;私有格式:数组<字符串>= ['DD-MM-YYYY', 'YYYY/MM/DD', 'DD.MM.YYYY', 'shortDate'];私有格式 = this.formats[0];私人日期选项:任何 = {格式年份:'YY',起始日:1};私人开放:布尔=假;公共 getDate():number {返回 this.fromDate.getDate() ||新日期().getTime();}}

    我想通过单击搜索详细信息页面中的确定按钮来访问开始日期和结束日期.我该怎么办?

    解决方案

    在您的子组件中将 EventEmitter 注册为 @Output:

    @Output() onDatePicked = new EventEmitter();

    点击时产生价值:

    public pickDate(date: any): void {this.onDatePicked.emit(date);}

    侦听父组件模板中的事件:

    <calendar (onDatePicked)=doSomething($event)"></calendar>

    在父组件中:

    public doSomething(date: any):void {console.log('选择日期:', 日期);}

    官方文档中也有很好的解释:组件交互.

    I have a component named search_detail which has another component inside it named calendar,

    SearchDetail_component.html

     <li class="date">
       <div class="btn-group dropdown" [class.open]="DatedropdownOpened">
       <button type="button" (click)="DatedropdownOpened = !DatedropdownOpened"   class="btn btn-default dropdown-toggle" data-toggle="dropdown"
    aria-haspopup="true" [attr.aria-expanded]="dropdownOpened ? 'true': 'false'">
          Date <span class="caret"></span>
      </button>
      <ul class="dropdown-menu default-dropdown">
         <calendar  ></calendar>
         <button > Cancel </button>
         <button (click)="setDate(category)"> Ok </button>
       </ul>                            
     </div>
    </li>
    

    SearchDetail_component.ts

    import 'rxjs/add/observable/fromEvent';
    @Component({
        selector: 'searchDetail',
        templateUrl: './search_detail.component.html',
        moduleId: module.id
    
    })
    

    Calendar.component.ts

    import { Component, Input} from '@angular/core'; 
    @Component({
        moduleId:module.id,
        selector: 'calendar',
        templateUrl: './calendar.component.html'
    })
    
        export class CalendarComponent{
          public fromDate:Date = new Date();    
          private toDate:Date = new Date();
          private events:Array<any>;
          private tomorrow:Date;
          private afterTomorrow:Date;
          private formats:Array<string> = ['DD-MM-YYYY', 'YYYY/MM/DD', 'DD.MM.YYYY', 'shortDate'];
          private format = this.formats[0];
          private dateOptions:any = {
            formatYear: 'YY',
            startingDay: 1
          };
          private opened:boolean = false;
    
          public getDate():number {
            return this.fromDate.getDate()  || new Date().getTime();
          }
        }
    

    I want to access the startdate and enddate on click of the ok button in the search detail page. how can i do?

    解决方案

    Register the EventEmitter in your child component as the @Output:

    @Output() onDatePicked = new EventEmitter<any>();
    

    Emit value on click:

    public pickDate(date: any): void {
        this.onDatePicked.emit(date);
    }
    

    Listen for the events in your parent component's template:

    <div>
        <calendar (onDatePicked)="doSomething($event)"></calendar>
    </div>
    

    and in the parent component:

    public doSomething(date: any):void {
        console.log('Picked date: ', date);
    }
    

    It's also well explained in the official docs: Component interaction.

    这篇关于如何将数据从子组件传递到父组件 Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆