【问题标题】:Angular2 bootstrap modal with child component带有子组件的Angular2引导模式
【发布时间】:2017-12-12 18:41:47
【问题描述】:

我想使用带有子组件的 Ng Bootstrap Modal 作为模态体。 我不确定我将如何实现这一目标......

export class ParentComponent {
   @ViewChild("modal") private engineModal: TemplateRef<any>;
   dialog: NgbModalRef | null;

   constructor(private modalService: NgbModal) {}

   openModal(): void {
      this.dialog = this.modalService.open(this.engineModal);
   }

   cancelModal (): void {
      if ( this.dialog ) {
         this.dialog.dismiss();
         this.dialog = null;
      }
   }
}

而且,我知道这是错误的,但最终我想做这样的事情,

<ng-template #modal>
  <app-child-component></app-child-component>
</ng-template>

子组件有很多输入和输出变量,那么这里最好的方法是什么?

【问题讨论】:

标签: angular bootstrap-modal angular2-template ng-bootstrap ng-modal


【解决方案1】:

您可以尝试内容投影(熟悉 AngularJS 的人可以使用嵌入)。

您可以像这样创建自定义模式:

<div class="modal-body"> 
  <ng-content select="child-body"></ng-content>
</div>

然后你根据这个自定义模态创建子模态

<custom-modal>
  <child-body>
     {...}
  </child-body>
</custom-modal>

基本上你在子体标签之间写的内容将被复制到 ng-content 位置的自定义模态元素中。

在此处阅读有关内容投影的更多信息:

https://angular-2-training-book.rangle.io/handout/components/projection.html

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您好,是的,您是对的,您必须使用 ViewChild 来执行此操作,然后按照描述创建一个使用 open 函数的 open 函数:

    open(engineModal) {
       console.log(this.engineModal) // global modal info from ViewChild
       console.log(engineModal) // modal info used for the function
       this.modalService.open(engineModal).result.then((result) => {
          this.closeResult = `Closed with: ${result}`;
       }, (reason) => {
       this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
    

    查看 .ts 示例 https://ng-bootstrap.github.io/#/components/modal/examples

    你只需要这样做,你现在就可以玩你的模态了

    编辑:访问 ng-template 中的子组件:// 不好的方式

    @ViewChild(AppChildComponent) appChildComponent: AppChildComponent
    this.engineModal.appChildComponent.function() 
    

    => 我会亲自将 ng-template 封装在 ModalInfoComponent、ModalNewsComponent html 中并直接使用我的答案;)

    【讨论】:

    • 好吧,我认为这行不通,我正在尝试动态加载模态,并且它使用的是 ng 模板.....
    • 将 ng-template 封装在自己的组件中怎么样?然后您可以访问您的组件和模态。这样您就可以重写子模态的行为。这是你需要的吗?
    • 我认为这将是最好的方式,您将ng-template封装在组件中是什么意思?
    • 我的意思是你用你的输入、输出来创建你的组件......并且在里面你会有你的 ng-template ;) 像这样 这里是特定的html
    【解决方案3】:

    您可以通过将内容作为输入属性传递来投影内容

    <button class="btn btn-lg btn-outline-primary" (click)="open(element.innerHTML)">Launch demo modal</button>
    <div #element style="display:none">>
      SOME THING PROJECTED
    </div>
    

    您的模态组件应具有以下modal-body

    <div class="modal-body">
      <div [innerHTML]="modalBody">
      </div>
    </div>
    

    具有以下输入属性

    @Input() modalBody:string;
    

    更新1:更改父级中@Input()属性的值可以如下完成,

    setTimeout(()=>{
        this.modalRef.componentInstance.name = 'Opened After sometime';
    },1000)
    

    LIVE DEMO

    【讨论】:

    • 是的,我同意,但我只是想知道是否有办法加载子组件,因为子组件与父组件有交互
    • by load the child component 你是什么意思?你能解释一下吗
    • 好像我必须创建一个模态组件。我的意思是这很好,但是我已经定义了子组件,它从父组件接收大约 12 个输入并有 4 个事件发射器输出。我可以让他们通过模态组件,但我想知道是否还有其他方法
    • @PowerLove 所以你不想动态加载模式?
    • 我想我在问题中遗漏了很多细节......对不起,所以父组件中的变量会动态变化,这会改变子组件中的输入
    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2016-08-02
    • 2017-07-10
    • 2016-07-11
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多