Use dialofRef to pass config between components in Angular 4

时间:2017-12-18 07:13:28

标签: angular angular-material

I'm using angular-material in Angular 4 and I want after setting the config (data, id) in my first component to pass it to another component so that I can read the properties.

In my (news-feed.component.ts) I have this function:

editPost(id,item) {
    const config = new MatDialogConfig();
    config.id = id;
    config.data = item;
    const dialogRef: MatDialogRef<PostComponent> = this.dialog.open(PostComponent, config);
    dialogRef.componentInstance.editedPost(config);
}

And in my (post.component.ts):

editedPost(config) {
    console.log(config.data);
}

And this is what I got in my console: enter image description here

My data is (das) word, that's mean it can read property 'data', and when I press any key the error repeated itself in console. What I've missed here?

1 个答案:

答案 0 :(得分:0)

我使用dialogRef找到了另一种解决方案。

在(news-feed.component.ts)中:

editPost(id,item) {
        const dialogRef: MatDialogRef<PostComponent> = this.dialog.open(PostComponent);
        dialogRef.componentInstance.myPost = item;
        dialogRef.componentInstance.myId = id;
    }

在我的(post.component.ts)中:

public myPost: any;
public myId: any;

editedPost() {
        console.log(this.myPost);
        console.log(this.myId);
        return true;
    }