【问题标题】:How to clear form inside of dialog material angular 2如何清除对话框材料角2内的表格
【发布时间】:2017-05-31 12:34:19
【问题描述】:

我在 angular 2 材质上创建了一个对话框。我打开模态如下:

我的组件

....
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 ();它奏效了。但是角材料 2 的工作方式不同。

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

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

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

....

谁能帮帮我?

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    看看我创建的 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() 方法完全休息

    【讨论】:

    • 感谢您的回答。 this.f.restForm() 不工作。我检查了 openDialog() 中没有表单的实例。这可能就是他无法重置的原因。我不知道如何获取角度材质对话框内的表单实例。这似乎是一个范围问题
    • 我刚刚创建了一个 plunker 来复制问题,我会尝试解决问题并通过更新此答案回复您。
    • @DaniloSantos 现在检查我的更新答案!如果这对您有帮助,请确保选择它作为答案:)
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 2021-11-27
    • 2018-08-31
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    相关资源
    最近更新 更多