2017-04-27 57 views
0

我需要從子組件獲取數據。我的孩子組件有流行的形式。我如何將全部細節傳遞給父組件。 我父組件TS文件是如何將角色2中的值從子元素傳遞給父元素?

import { Component, OnInit } from '@angular/core'; 
import {MdDialog, MdDialogRef} from '@angular/material'; 

@Component({ 
    selector: 'app-vehicle-relocate', 
    templateUrl: './vehicle-relocate.component.html', 
    styleUrls: ['./vehicle-relocate.component.css'], 
}) 
export class VehicleRelocateComponent implements OnInit { 

    lat: number = 11.074529; 
    lng: number = 78.003917; 
    zoom: number = 14; 

    ngOnInit() { 
    } 

    selectedOption: string; 

    constructor(public dialog: MdDialog) {} 

    openDialog() { 
    let dialogRef = this.dialog.open(); 
    dialogRef.afterClosed().subscribe(result => { 
     this.selectedOption = result; 
    }); 
    } 
} 

我的孩子部件是在父組件

import { Component, OnInit, Input } from '@angular/core'; 
import {MdDialog, MdDialogRef} from '@angular/material'; 

@Component({ 
    selector: 'app-relocate-form', 
    templateUrl: './relocate-form.component.html', 
    styleUrls: ['./relocate-form.component.css'] 
}) 
export class RelocateFormComponent implements OnInit { 

    constructor(public dialogRef: MdDialogRef<RelocateFormComponent>) {} 
    @Input() title:string; 

    ngOnInit() { 
    } 

} 
+0

它會幫忙嗎? https://github.com/angular/material2/blob/master/src/demo-app/dialog/dialog-demo.ts –

+0

哪一個是兒童組件? –

+0

第二個是子組件 – niranchan

回答

1

您可以添加Output你的孩子組成。 例如:@Output() notifySubmit : EventEmitter<any> = new EventEmitter<any>()(如果你不想'任何',你可以放任何你想要的類型)。

然後,當你在你的子組件提交表單,你與Output通知父:

this.notifySubmit.emit(myValues) 

現在你必須接受父組件的事件。當您從VehicleRelocateComponent呼叫RelocateFormComponent時,您需要將功能傳遞給Output

<app-relocate-form (notifySubmit)="myFunction($event)" ... ></app-relocate-form> 

myFunction($event)必須是在父組件。 $event參數等於您使用this.notifySubmit.emit(myValues)發送的內容。

相關問題