eth*_*han 39 angular-material angular-material2 angular
对于mdDialog,我如何传入变量?具体来说,如何将Angular服务注入对话框组件?
poi*_*dar 58
对于传递变量,您可以从MdDialog.open()方法调用中返回的MdDialogRef实例中获取对话框中打开的组件的实例.
dialogRef = this.dialog.open(PizzaDialog, config)
dialogRef.componentInstance.<property_name>
Run Code Online (Sandbox Code Playgroud)
来自github material2 docs的 Modified Pizza
@Component({
selector: 'pizza-component',
template: `
<button type="button" (click)="openDialog()">Open dialog</button>
`
})
export class PizzaComponent {
constructor(public dialog: MdDialog) { }
openDialog() {
let config = new MdDialogConfig();
let dialogRef:MdDialogRef<PizzaDialog> = this.dialog.open(PizzaDialog, config);
dialogRef.componentInstance.name = "Ham and Pineapple";
dialogRef.componentInstance.size = "Large";
}
}
@Component({
selector: 'pizza-dialog',
template: `
<h2>{{name}}</h2>
<p>Size: {{size}}</p>
<button type="button" (click)="dialogRef.close('yes')">Yes</button>
<button type="button" (click)="dialogRef.close('no')">No</button>
`
})
export class PizzaDialog {
name:string;
size:string;
constructor(public dialogRef: MdDialogRef<PizzaDialog>) { }
}
Run Code Online (Sandbox Code Playgroud)
Thi*_*PXP 39
Material2 beta.2
该dialog.open()函数采用第二个参数config(MdDialogConfig),您可以在其中指定任何data对象.
this.dialog.open(YourComponent, {
data: {
anyProperty: "myValue"
}
});
Run Code Online (Sandbox Code Playgroud)
然后,您可以从用于对话框窗口的组件中检索此对象.
export class YourDialogComponent {
constructor(public dialogRef: MdDialogRef<YourComponent>) {
console.log('data', this.dialogRef.config.data);
}
}
Run Code Online (Sandbox Code Playgroud)
更新:beta.3
上面的答案适用于2.0.0-beta.2Material2 的版本.如果您正在使用2.0.0-beta.3,则该config属性已被删除MdDialogRef.您可以使用MD_DIALOG_DATA打开的组件注入该值.
新的进口报表
import {MdDialog, MdDialogRef, MdDialogConfig, MD_DIALOG_DATA} from '@angular/material';
Run Code Online (Sandbox Code Playgroud)
开放式对话
this.dialog.open(YourComponent, {
data: {
anyProperty: "myValue"
}
});
Run Code Online (Sandbox Code Playgroud)
从DialogRef组件中检索数据
export class YourDialogComponent {
constructor(
public dialogRef: MdDialogRef<YourDialogComponent>,
@Inject(MD_DIALOG_DATA) public data: any) {
console.log('data', this.data);
}
}
Run Code Online (Sandbox Code Playgroud)
来自https://material.angular.io/components/dialog/overview上的官方文档
使用Dialog组件共享数据.
如果要与对话框共享数据,可以使用data选项将信息传递给对话框组件.
let dialogRef = dialog.open(YourDialog, {
data: 'your data',
});
Run Code Online (Sandbox Code Playgroud)
要访问对话框组件中的数据,必须使用MD_DIALOG_DATA注入令牌:
import {Component, Inject} from '@angular/core';
import {MD_DIALOG_DATA} from '@angular/material';
@Component({
selector: 'your-dialog',
template: 'passed in {{ data }}',
})
export class YourDialog {
constructor(@Inject(MD_DIALOG_DATA) public data: any) { }
}
Run Code Online (Sandbox Code Playgroud)
要给出更新的答案以适应从“Md”到“Mat”的更新:
要打开包含数据的对话框,请传入数据对象:
this.dialog.open(YourComponent, {
data: {
anyProperty: "myValue"
}
});
Run Code Online (Sandbox Code Playgroud)
要在对话框中检索该数据:
import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
export class YourDialogComponent {
constructor(
public dialogRef: MatDialogRef<YourDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
console.log('data passed in is:', this.data);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我做到的.
pizza.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class PizzaService {
getTopping(): string {
return "Mushrooms"
}
}
Run Code Online (Sandbox Code Playgroud)
pizzaDialog.component.ts
import { Component } from '@angular/core';
import { MdDialogRef} from '@angular/material';
import {PizzaService} from './pizza.service';
@Component({
selector: 'pizza-dialog',
template: `{{pizzaTopping}}
<button type="button" (click)="dialogRef.close('yes')">Yes</button>
<button type="button" (click)="dialogRef.close('no')">No</button>
`,
providers: [PizzaService]
})
export class PizzaDialog {
pizzaTopping: string;
constructor(public dialogRef: MdDialogRef<PizzaDialog>, private pizzaService: PizzaService) { };
ngOnInit(): void {
this.pizzaTopping = this.pizzaService.getTopping()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32871 次 |
| 最近记录: |