【问题标题】:Submit from the angular material form inside dialog从对话框内的角度材料表单提交
【发布时间】:2018-08-13 13:37:23
【问题描述】:

我在对话框中有一个表单。

mainComponent

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

HTML

<form (ngSubmit)="setRule(f)" #f="ngForm">
        <md-form-field>
            <input matInput placeholder="Name" name="name" ngModel>
        </md-form-field>
    .....

      <button mat-raised-button mat-dialog-close>Confirm</button>
      <button mat-raised-button mat-dialog-close>Cancel</button>

</form>

我有一个没有对话框的表单,它对confirmcancel 都很好,现在我决定在对话框中打开表单。但我无法获得setRule(f) 的值。

ModelComponent

setRule(f) {
  console.log(f);
}

有人可以告诉我我错过了什么。 任何帮助表示赞赏。

谢谢。

【问题讨论】:

    标签: javascript angular angular-material2


    【解决方案1】:

    Open Dialog 代码是否添加到定义 setRule() 方法的同一组件中?如是。应该可以的。

    服务:

    private formSubmitEvent = new Subject<any>();
    public formSubmitEvent$ = this.formSubmitEvent.asObservable();
    

    主要组件:

    setRule(){
       service.formSubmitEvent.next(values);
    }
    

    模型组件:

    ngOninit(){
        service.formSubmitEvent$.subscribe(values=>{
           console.log(values);
        })
    }
    

    【讨论】:

    • setRule() 在 modelComponent 中,openDialog 在主要组件中,如角度材料文档中所述
    • 然后是它的 2 个不同的组件。您需要使用 Subject/Behavior Subject 将值从主组件发送到 modelComponent。
    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 2021-10-08
    • 2021-11-02
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    相关资源
    最近更新 更多