2017-03-27 30 views
1

我想將數據傳遞給對話框,但它應該完成的方式(根據互聯網上找到的幾個示例,如Using MdDialogConfig data on Angular 2)不起作用。將數據傳遞給MdDialog不再有效

我收到以下錯誤:

Property 'data' does not exist on type 'MdDialogConfig' 

當我看MdDialogConfig這個屬性確實不存在的(再)

的代碼,我使用的,可以發現作爲公認的例子回答上面的話題,但它不起作用。示例代碼是:

const config = new MdDialogConfig(); 

config.data = [ 
    // for example: 
    'value 1', 
    'value 2' 
]; 

const dialogRef = this.dialog.open(DialogComponent, config); 

掃描剩下的代碼,我沒有看到其他方式直接傳遞數據。 我可以回到使用單獨的服務將數據從組件傳遞到對話框,但這遠非理想。

有沒有正確的方法將數據傳遞給對話框?

我使用的版本2.0.0-beta.1

+0

我看到有一個更新的版本可用,似乎有數據propery。在這個時候檢查出來 –

+0

是的,這是訣竅。不幸的是,包含主題的api也改變了。令人沮喪地做出比我預期的更多的變化...... –

+0

這是4.2.3中的一個問題。 – isherwood

回答

0

您可以創建自己的充氣組件(注意細節場)

export class DialogDetailComponent implements OnInit { 

    @Input() detail: Detail; 

    constructor(public dialogRef: MdDialogRef<DialogDetailComponent>) { 
    } 

    ngOnInit() { 
    } 

    close() { 
     this.dialogRef.close(); 
    } 

} 
export interface Detail { } 

,並創建一個開放,在您的服務對話框的方法

openDialogDetail(c: Type<DialogDetailComponent>, detail: Detail, afterClosed?: (data?) => any, type?: number, width: string = "80%", height: string = "80%", disableClose = false) { 

    let dialogRef = this.openDialog(c, afterClosed, width, height, disableClose); 

    if (detail) 
     dialogRef.componentInstance.detail = detail; 

    return dialogRef; 

} 
+0

謝謝。我遇到的問題是在2.0.0-beta.1中data屬性沒有實現。事實證明,在2.0.0-beta.2中它已經回來了。 –

+0

很高興聽到。不過,如果您需要更多靈活性,則可以使用此策略。再見! –