angular - 如何清除对话框 Material Angular 2内的表格

标签 angular angular-material

我在 Angular 2 Material 上创建了一个对话框。我打开模式如下:

我的组件

....
private refDialog;
@ViewChild('display') display;

openDialog() {
    this.refDialog= this.dialog.open(this.display);
}
....

我的html

<ng-template #display>
    <form (ngSubmit)="onSubmit(f)" #f="ngForm">
        <div class='col-md-12'>
            <md-input-container>
                <input mdInput placeholder="Name" name="name" [(ngModel)]="product.name" #name="ngModel">
            </md-input-container>
        </div>

        .....

    </form>
</ng-template>

我需要在打开对话框之前清除表单。我想尽可能简单地做到这一点。我正在使用 primeNG 对话框,并使用它来使用 this.f.reset ();它奏效了。但有 Angular Material 2 的工作原理不同。

我尝试了一切但没有成功。这对我不起作用:

....
private refDialog;
@ViewChild('display') display;
@ViewChild('f') f;

openDialog() {
    this.f.reset();
    this.refDialog= this.dialog.open(this.display);
}

....

有人可以帮我吗?

最佳答案

看看我创建的 Plunker 演示:HERE

这是对话框组件:

export class DialogComponent implements OnInit{
  @ViewChild('myform') myform;
  constructor(public dialogRef: MdDialogRef<DialogComponent>){}
  ngOnInit() {
  this.myform.resetForm();
  }
}

这是根组件:

openDialog(){
  this.dialog.open(DialogComponent);
}

正如您在 GIF 中看到的那样,每次清除 HTML 中的硬编码输入值以及表单脏状态时,表单都会被清除,以便使用 resetForm() 方法 enter image description here

关于angular - 如何清除对话框 Material Angular 2内的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44284977/

相关文章:

angular - 如何从一个 Angular 库到另一个 Angular 库进行通信?

javascript - 如何在 Angular 7 中使用 goJs TreeModel

angular - 如何将带有按钮的列添加到带有循环的表格中?

html - Angular Material 将表格转换为小屏幕上的方框

html - 如何在组件选择器上添加禁用属性?

Angular/RxJs 6 - 组合路由参数和查询参数不再有效

angular - 无法更改 Angular Material 选择占位符颜色

angular - mat autocomplete - 如何设置未找到的选项

javascript - typescript 反引号语法突出显示

material-design - 使用角 Material 创建一个类似砖石的 md-grid-list