2017-07-25 21 views
0

子コンポーネントから入力フィールドを消去しようとすると、親コンポーネントのメソッドdelete()をアクティブにする@Output情報によって転送されます。角2コンポーネントの子でメソッドのメソッドをコンポーネントの親に送信する方法

ありがとうございました

+3

例は[documentation](https://stackoverflow.com/documentation/angular/10836/sharing-data-amon-components/32497/sending-data-from-child-to-parent-via)にあります。 -output-event-emitter#t = 201707251354497555945) – Nehal

答えて

0

あなたはEventEmitter@Outputを使用していることacomplishことができます。

をスニペットを以下では、あなたが目的のデータを渡すためにpassDataToParent()関数を呼び出すことができます。

child.component.ts

import { Component, OnInit, Output, EventEmitter } from '@angular/core'; 
    @Component({ 
     selector: 'app-child-component', 
     templateUrl: './child.component.html', 
     styleUrls: ['./child.component.css'] 
    }) 

    export class ChildComponent implements OnInit { 
     // ******** Important part ****** 
     @Output() emitter: EventEmitter<any[]> = new EventEmitter(); 
     dataToEmit : any = "data to pass to parent component"; 

      constructor() {} 

      ngOnInit() { } 

      //Call this function to pass data 
      passDataToParent() { 
      this.emitter.emit(this.dataToEmit); 
      } 
    } 

parent.component.ts親HTMLで最後に

import { Component, OnInit, ViewChild } from '@angular/core'; 
    import { ChildComponent } from './child-component'; 

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

    export class ParentComponent implements OnInit { 

     // ******** Get reference of child component ****** 
     @ViewChild(ChildComponent) child : ChildComponent ; 

      constructor() {} 

      ngOnInit() { } 

      receiveDataFromChild(data) { 
       console.log(data); 
      } 
    } 

parent.component.html

<app-child (emitter)="receiveDataFromChild($event)"></app-child > 

希望します。

+0

あなたはあなたです!私はすでに本を読んで始めている!あなたが助けてくれた^^ –

0

eventEmitterを使用してください。ここでは選択したアイテムの値を削除しましたが、アイテムを削除できます。非同期レンダリング問題の場合、uはTrueの出力EventEmitterを使用できます。

I made example here

@Input() items: any[]; 
    @Input() selectedItem: any; 
    keys: string[] = []; 

    @Input() sorting: string; 

    @Output() selectedItemChange = new EventEmitter<any>(true); 
関連する問題