子コンポーネントからデータを取得する必要があります。私の子コンポーネントは、popuのフォームを持っています。完全な詳細を親コンポーネントに渡すにはどうすればよいですか。私の親コンポーネントのtsファイルは
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() {
}
}
Output
子コンポーネントにを追加できます。例:( @Output() notifySubmit : EventEmitter<any> = new EventEmitter<any>()
「any」が必要ない場合は、任意のタイプを入力できます)。
次に、子コンポーネントでフォームを送信するときに、Output
:を使用して親に通知する必要があります。
this.notifySubmit.emit(myValues)
次に、親コンポーネントでイベントを受信する必要があります。RelocateFormComponent
からを呼び出すときはVehicleRelocateComponent
、関数をに渡す必要がありますOutput
。
<app-relocate-form (notifySubmit)="myFunction($event)" ... ></app-relocate-form>
myFunction($event)
親コンポーネントに含まれている必要があります。$event
パラメータは、あなたが送信したものに等しいですthis.notifySubmit.emit(myValues)
。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加