How to pass parameteres to modal?

前端 未结 6 2022
既然无缘
既然无缘 2021-02-07 10:29

That\'s the way I use the ng2-bootstrap modal:

import {Component} from \'@angular/core\';
import {NgbModal} from \'@ng-bootstrap/ng-bootstrap\';

@Component({
           


        
6条回答
  •  情歌与酒
    2021-02-07 11:00

    There is a better approach to handle data in the modal if you need to have data immediately after modal construction. Using angular injector technic.

    1. Define a model for your data.
    class CustomModalOptions {
      stringProp: string; 
      numberProp: number;
    }
    
    1. Build your modal somewhere in your component:
    this.modal.open(MyModal, {
      injector: Injector.create([{
        provide: CustomModalOptions, useValue: { stringProp: "foo bar", numberProp: 17 }
      }], this.injector)
    });
    
    1. Handle data retrieve in your Modal Component
    @Component({ ... })
    class MyModal {
      constructor(private options: CustomModalOptions) {
        console.log(this.options.stringProp);
        console.log(this.options.numberProp);
      }
    }
    

提交回复
热议问题