Angular 2 材质对话框:在对话框关闭时刷新父级

ste*_*eno 4 modal-dialog angular-material2 angular

我正在使用 angular material 2 对话框将项目添加到列表中。我有一个包含列表(表)的组件(AuthorListComponent)。该组件有一个名为 getAuthors 的函数,它进行一个休息调用并在模板中呈现一个表格。我还有一个按钮,可以打开用于添加新项目的 angular material 2 对话框:

https://github.com/angular/material2/blob/master/src/lib/dialog/README.md

我无法弄清楚的是,当保存对话框中的项目并关闭对话框时,我希望刷新 AuthorListComponent 以便显示新添加的项目。我想我必须在某处使用事件发射器来调用 getAuthors 函数,但是对话框不使用我可以将事件附加到的指令/html 元素。

这是我的模式服务:

import { Observable } from 'rxjs/Rx';
import { AuthorComponent } from '../author/author.component';
import { MdDialogRef, MdDialog, MdDialogConfig } from '@angular/material';
import { Injectable, ViewContainerRef } from '@angular/core';

@Injectable()
export class DialogService {

    constructor(private dialog: MdDialog) { }

    public openDialog(viewContainerRef: ViewContainerRef): Observable<boolean> {
        let dialogRef: MdDialogRef<AuthorComponent>;
        let config = new MdDialogConfig();
        config.viewContainerRef = viewContainerRef;
        config.disableClose = true;
        config.width = '600px';
        dialogRef = this.dialog.open(AuthorComponent, config);
        return dialogRef.afterClosed();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是在对话框中加载的作者组件:

import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MdDialogRef } from '@angular/material';

import { AuthorService } from './author.service';

@Component({
    moduleId: module.id,
    selector: 'author-comp',
    templateUrl: 'author.component.html'
})

export class AuthorComponent implements OnInit {
    authorId: number;
    author = {};

    constructor(
        private route: ActivatedRoute, 
        private authorService: AuthorService,
        private router: Router,
        public dialogRef: MdDialogRef<AuthorComponent>
    ) 
    { }

    ngOnInit() {
        this.authorId = this.route.snapshot.params['AuthorId'];
        if (this.authorId)
        {
            this.authorService.getAuthor(this.authorId)
                .then((author) => {
                    this.author = author;
                    })
                .catch((error) => {
                    throw ('ngOnInit-getAuthor: ' + error);
                });
        }
    }

    saveAuthor(Author: any) {
        return this.authorService.saveAuthor(Author)
            .then(() => {
                this.dialogRef.close();
            })
            .catch(error => {
                throw ('saveAuthor-component: ' + error);
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是打开对话框的作者列表组件:

import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import { AuthorService } from './index';
import { DialogService } from '../shared/dialog.service';


@Component({
  moduleId: module.id,
  selector: 'author-list-comp',
  templateUrl: 'author-list.component.html'
})

export class AuthorListComponent implements OnInit {
  authors: any[];

  constructor(
    private authorService: AuthorService,
    private dialogService: DialogService,
    private viewContainerRef: ViewContainerRef,
    private router: Router
  )
  {}

  getAuthors() {
      this.authorService.getAuthors()
      .then((authors) => {
        this.authors = authors;        
      })
      .catch((error) => {
        throw('ngOnInit-getAuthors: ' + error)
      })
  }

  ngOnInit(){
    this.getAuthors();
  }

  public openDialog() {
    this.dialogService.openDialog(this.viewContainerRef);
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我想我需要从对话服务或作者组件调用上面组件中的 getAuthors 函数。我想到的另一种方式就是以某种方式使用 router.navigate 。

任何帮助表示赞赏!

小智 5

在对话框关闭时刷新父级的最简单方法:

this.dialog.open(DialogComponent)
  .afterClosed()
  .subscribe(() => this.refreshParent());
Run Code Online (Sandbox Code Playgroud)