Angular6 “角度材质模式”对话框无法将事件传递给父对象?

Angular6 “角度材质模式”对话框无法将事件传递给父对象?,angular6,angular-material-6,Angular6,Angular Material 6,我有一个组件,它有一个子组件。子组件有一个按钮,用于打开“材质”对话框。 在对话框中,我们有表单、用户名、密码和提交按钮。当我提交时,我正在调用后端RESTAPI 这在子组件中被调用: dialogRef.afterClosed().subscribe(result => { console.log("result", result); this.onModalClosePassData.emit(result);//emit to parent });

我有一个组件,它有一个子组件。子组件有一个按钮,用于打开“材质”对话框。 在对话框中,我们有表单、用户名、密码和提交按钮。当我提交时,我正在调用后端RESTAPI

这在子组件中被调用:

dialogRef.afterClosed().subscribe(result => {
      console.log("result", result);
      this.onModalClosePassData.emit(result);//emit to parent
    });
正在将事件发送到父级updateComment()正在被调用,我可以在控制台中看到数据

但当我填写表格并点击提交时。它调用submitForm方法,这是一个异步调用,我在成功登录后关闭对话框,但事件并没有显示。未调用updateComment()

请参阅完整代码:

parent component.html

<ng-template #showTextOnly>
        <child-component [branch]="releaseBranch" [date]="dateString"
                  (onModalClosePassData)="updateComment($event)">
        </child-component>
</ng-template>

parent component.ts

//This method is getting called when i click on backDrop, 
parent component.html
父组件.ts
//当我点击背景时,这个方法被调用,
但是,如果我成功登录,则不会调用此命令 updateComment(事件:任意){ consile.log(事件); }

child-component.html
登录
子组件
导出类ChildComponent实现OnInit{
@Output()onModalClosePassData=new EventEmitter();
构造函数(公共对话框:MatDialog){}
openDialog():void{
const dialogConfig=new MatDialogConfig();
dialogConfig.disableClose=false;
dialogConfig.autoFocus=false;
dialogConfig.hasBackground=true;
dialogConfig.width='300px';
dialogConfig.autoFocus=true;
dialogConfig.data={branch:this.branch,日期:this.date};
const dialogRef=this.dialog.open(LoginDialog,dialogConfig);
dialogRef.afterClosed().subscribe(结果=>{
log(“result”,result);//在这两种情况下都会调用result
this.onModalClosePassData.emit(结果);
});
}
}
LoginDialog.component.ts
从“@angular/material/DIALOG”导入{MatDialogRef,MAT_DIALOG_DATA};
导出类LoginDialog实现OnInit{
构造函数(私有loginService:loginService,公共dialogRef:MatDialogRef,
@注入(MAT_DIALOG_DATA)公共数据:any{}
public submitForm=(formValue:any)=>{
if(this.noteForm.valid){
让encryptData=btoa(`${formValue.username}:${formValue.password}`);
this.loginService.login(encryptData)
.订阅((响应:任意)=>{
如果(response.STATUS==“失败”){
}否则{
this.dialogRef.close(this.noteDetail);
}
})
}
}
}
LoginDialog.component.html
用户名
密码
提交

您没有显示
updateComment()
函数代码,您有两个不同版本的afterClosed侦听器-
this.onModalClosePassData.emit()
this.onModalClosePassData.emit(结果)
。这些细节可能很重要。@G.Tranter目前我正在updateComment()中打印传递的值
updateComment($event:any){console.log($event);}
。实际上
这个.onModalClosePassData.emit(result)
是正确的,因为我想在父组件中发送数据。如果用户单击Background,它将发送未定义的,如果成功登录,它将发送用户对象。在这两种情况下
console.log(“result”,result)正在打印,但当用户登录时。事件没有被触发,这就是为什么我可以在
updateComment()
中看到任何东西。您没有显示
updateComment()
函数代码,并且您有两个不同版本的afterClosed侦听器-
this.onModalClosePassData.emit()
this.onModalClosePassData.emit(结果)
。这些细节可能很重要。@G.Tranter目前我正在updateComment()中打印传递的值
updateComment($event:any){console.log($event);}
。实际上
这个.onModalClosePassData.emit(result)
是正确的,因为我想在父组件中发送数据。如果用户单击Background,它将发送未定义的,如果成功登录,它将发送用户对象。在这两种情况下
console.log(“result”,result)正在打印,但当用户登录时。事件未被触发,这就是为什么我可以在
updateComment()
中看到任何内容。
child-component.html

<button class="btn btn-default" (click)="openDialog()">Login</button>

child-component.ts

export class ChildComponent implements OnInit {
    @Output() onModalClosePassData = new EventEmitter();
    constructor(public dialog: MatDialog) { }
    openDialog(): void {
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = false;
    dialogConfig.autoFocus = false;
    dialogConfig.hasBackdrop= true;
    dialogConfig.width = '300px';
    dialogConfig.autoFocus=true;
    dialogConfig.data = {branch: this.branch, date: this.date};
    const dialogRef = this.dialog.open(LoginDialog, dialogConfig);

    dialogRef.afterClosed().subscribe(result => {
      console.log("result", result); //result is getting called in both cases
      this.onModalClosePassData.emit(result);
    });
 }
}

LoginDialog.component.ts

import {MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
export class LoginDialog implements OnInit{
    constructor(private loginService: LoginService, public dialogRef: MatDialogRef<LoginDialog>,
      @Inject(MAT_DIALOG_DATA) public data: any) {}
      public submitForm = (formValue: any) => {
      if (this.noteForm.valid) {
        let encryptData = btoa(`${formValue.username}:${formValue.password}`);
        this.loginService.login(encryptData)
         .subscribe((response:any)=>{
             if(response.STATUS === "FAILED"){
             } else {
              this.dialogRef.close(this.noteDetail);
             }
        })
      }
    }

}
LoginDialog.component.html

<form [formGroup]="noteForm" autocomplete="off" novalidate (ngSubmit)="submitForm(noteForm.value)">
<mat-dialog-content class="mat-typography">
        <mat-form-field>
                <mat-label>User Name</mat-label>
                <input matInput type="text" formControlName="username" id="username">
        </mat-form-field>
        <mat-form-field>
                <mat-label>Password</mat-label>
                <input matInput type="password" formControlName="password">
        </mat-form-field>
</mat-dialog-content>
<mat-dialog-actions align="center">
  <button mat-raised-button color="primary" [disabled]="!noteForm.valid">Submit</button>
</mat-dialog-actions>
</form>