kar*_*rty 5 angular-cli angular
我有一个对话框组件和应用程序组件,用于实现材质对话框。 这是应用程序组件的代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
对话框组件是实现表单的地方,我需要获取表单值并在此处接收它。我尝试使用角度输入和输出来实现此目的,但是精巧的工作因为没有孩子和父母的交流。这是对话框组件
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');
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用真正实现通信订阅到@Output通过MatDialogRef<EditDialog>。在某些情况下,您可能需要在关闭对话框之前从对话框获取数据。因此,this.DialogRef.afterClosed()由于必须先关闭对话框才能获取数据,因此无法使用该功能。
在您的DialogComponent上:
export class DialogComponent {
@Output() submitClicked = new EventEmitter<any>();
constructor(public dialogRef: MatDialogRef<DialogComponent>){}
saveMessage() {
const data = 'Your data';
this.submitClicked.emit(data);
}
closeDialog() {
this.dialogRef.close();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的AppComponent上:
addItem() {
this.DialogRef = this.dialog.open(DialogComponent);
this.DialogRef.componentInstance.submitClicked.subscribe(result => {
console.log('Got the data!', result);
});
}
Run Code Online (Sandbox Code Playgroud)
更好地确保unsubscribe()所有Subscription。这样的操作可以快速退订(如果不验证所涉及的数据):
const dialogSubmitSubscription = this.DialogRef.componentInstance.submitClicked
.subscribe(result => {
console.log('Got the data!', result);
dialogSubmitSubscription.unsubscribe();
});
Run Code Online (Sandbox Code Playgroud)
另外,如果需要,您总是可以从AppComponent与关闭对话框this.DialogRef.close()。
一个用例场景,如果您想在对话框中编辑一些数据,然后将编辑后的数据从对话框传递回组件。我使用了上面的示例,但我合并了答案以使其更易于理解。假设数据来自服务,此示例将数据从 mat-dialog 组件共享到同一文件中的应用程序组件。
// app.component.html
<div *ngFor="let a of appData">
<p>{{a.name}}</p>
<button> (click)="open(event, a)">edit</button>
</div>
Run Code Online (Sandbox Code Playgroud)
// app.component.ts
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { DataService } from './data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
appData: Array <any>;
constructor(private dataService: DataService, public dialog: MatDialog) {}
ngOnInit() {
this.dataService.getData()
.subscribe(res => {
//console.log(res);
this.appData = res;
})
}
public open(event, data) {
this.dialog.open(EditDialog, {
data: data,
}).afterClosed()
.subscribe(item => {
// Edited Data sent to App Component from Dialog
console.log(item);
});
}
}
@Component({
selector: 'edit-dialog',
template: `<span>Edit Data</span>
<mat-dialog-content>
<input matInput name="title" type="text" class="form-control" placeholder="Edit Name" [(ngModel)]="dataItem.name">
</mat-dialog-content>
<div>
<span><button mat-raised-button (click)="updateData()">Update Recipe</button></span>
</div>`,
})
export class EditDialog implements OnInit {
dataItem: any;
constructor(public dialogRef: MatDialogRef <EditDialog> , @Inject(MAT_DIALOG_DATA) public data: any, private dataService: DataService) {
this.dataItem = this.data;
}
public updateData() {
this.dialogRef.close(this.dataItem);
}
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7030 次 |
| 最近记录: |