简体   繁体   English

angular 材质对话框通过动态 function 点击保存时

[英]angular material dialog pass dynamic function when clicked on save

dialog.ts对话框.ts

export class DialogComponent {
    constructor(private service: RestService, private dialogRef: MatDialogRef<DialogComponent>) {
    }

    no() {
        this.dialogRef.close();
    }

    save() {
        const url = 'https://gorest.co.in/public-api/users'; 
        const options = {headers: HttpHeaders, params: HttpParams};

        const getResp: any = this.service.Get(url, options);       
        this.dialogRef.close();

    ///THIS save() FUNCTION HARDCODED, BUT I WANT TO MAKE RESTCALL DYNAMICALLY WHEN ANYONE CLICKS SAVE BUTTON IN DIALOG
    }

}

dialog.html对话框.html

<mat-dialog-actions>

    <button class="mat-raised-button"
            (click)="no()">
        no
    </button>

    <button class="mat-raised-button mat-primary"
            (click)="save()">
        save
    </button>

</mat-dialog-actions>

component1.ts组件1.ts

 getUsers() {

    const dialogConfig = new MatDialogConfig();

    const dialogRef = this.dialog.open(DialogComponent,
        dialogConfig, passSomeRestCall-1-FunctionSomehow());  
  }

component2.ts组件2.ts

 getEmployees() {

    const dialogConfig = new MatDialogConfig();

    const dialogRef = this.dialog.open(DialogComponent,
        dialogConfig, passSomeRestCall-2-FunctionSomehow());  
  }

Above 2 components have to make use of Dialog Component dynamically, currently above save() hardcoded with some restcall, but actually rest call needed when clicked on save() for both above components.以上 2 个组件必须动态使用 Dialog 组件,目前在 save() 上面用一些 restcall 硬编码,但实际上在单击上述两个组件的 save() 时需要 rest 调用。 So, in short, save() button should happen dynamic restcall based on the component.所以,简而言之,save() 按钮应该基于组件发生动态的restcall。

Could anyone please help me on above, I am very new to angular.任何人都可以在上面帮助我,我对 angular 很陌生。

EDIT:编辑:

 for (let i = 0; i < this.items.length; i++) {
      this.service.makeRestCall(this.items[i]);
    }

How do I pass above for loop logic in dialog component?如何在对话框组件中传递上述循环逻辑? I should be able to do some business logic dynamically inside save() based on component like below我应该能够根据下面的组件在 save() 中动态地执行一些业务逻辑

save(){
   dynamicMethod() // this should be passed somehow from a component
  this.dialogRef.close();
} 

You do not need to pass the callback method.您不需要传递回调方法。 It should be part of modal's parent.它应该是模态的父级的一部分。 Only you have need is to set @Output into the modal with type EventEmitter.只有您需要将 @Output 设置为 EventEmitter 类型的模态。 Like:喜欢:

@Output() save = new EventEmitter<boolean>();

On Save button just implement:在保存按钮上只需实现:

handleSave: void {
  this.save.emit(true);
}

Parent will listen this @Output and handle it properly. Parent 会监听这个@Output 并正确处理它。

dialogRef.componentInstance.save$.subscribe((res) => {
    // Here is your business logic 
  }
);

So:所以:

  1. If after processing is everything OK, you can use dialogRef in your parent and close the modal (dialogRef.close()).如果处理后一切正常,您可以在父级中使用 dialogRef 并关闭模式 (dialogRef.close())。
  2. If something is wrong, modal will not be closed.如果出现问题,模态将不会关闭。

Using this way, our modal will be free of business logic.使用这种方式,我们的模态将没有业务逻辑。 Especially important for generic modals, like confirmation, user inputs, ...对于通用模式尤其重要,例如确认、用户输入……

You can pass your url, options etc as data in your dialog component and pass it in save function like below.您可以将 url、选项等作为对话框组件中的数据传递,并将其传递到保存 function 中,如下所示。

UserComponent用户组件

 openDialog(): void {
    let ApiDetails = {
      url: 'https://gorest.co.in/public-api/users'
    }
    const dialogRef = this.dialog.open(DialogComponent,
      {
        data: ApiDetails
      });
  }

DialogComponent对话框组件

export class DialogComponent implements OnInit {

  constructor(
    private dialogRef: MatDialogRef<DialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any) {
  }

  no(){
    this.dialogRef.close()
  }

  save(){
    console.log(this.data) // this will have url and other details whatever you send from calling parent
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM