我有一个对话框组件和应用程序组件,其中实现了材质对话框。 这是应用程序组件的代码
import { Component } from '@angular/core';
import {VERSION, MatDialog, MatDialogRef} from '@angular/material';
import { DialogComponent } from '../dialog.component';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 5';
DialogRef: MatDialogRef<DialogComponent>;
constructor(private dialog: MatDialog) {}
ngOnInit() {
}
addItem() {
this.DialogRef = this.dialog.open(DialogComponent);
}
receiveMessageFromDialogComponent() {
// how to receive message from dialog component
}
closeDialog(): void {
this.DialogRef.close();
}
}
对话框组件是实现表单的地方,我需要在这里获取表单值并接收它。我尝试使用角度输入和输出来实现这一点,但是由于没有孩子和父母的交流,所以很有效。这是对话框组件
import { Component } from '@angular/core';
@Component({
template: `
<h1 mat-dialog-title>Add Item</h1>
<mat-dialog-content>
<mat-form-field class="example-full-width">
<input matInput placeholder="Item name here...">
</mat-form-field>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button (click)="saveMessage()">Add</button>
<button mat-button (click)="closeDialog()">Cancel</button>
</mat-dialog-actions>
`
})
export class DialogComponent {
saveMessage() {
console.log('how to send data to the app component');
}
closeDialog() {
console.log('how to close');
}
}