如何将参数传递给模态? [英] How to pass parameteres to modal?

查看:139
本文介绍了如何将参数传递给模态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我使用ng2-bootstrap模式的方式:

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

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

@Component({
  selector: 'add-customer-modal',
  template: `
    <template #test let-c="close" let-d="dismiss">
      <div class="modal-header">
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
      </div>
    </template>
    <button type="button" class="btn btn-primary btn-sm" (click)="open(test)"><i class="fa fa-plus"></i> <i class="fa fa-user-o"></i></button>
  `
})
export class AddCustomerModal {

  constructor(private modalService: NgbModal) {}

  open(content) {
    this.modalService.open(content, { size: 'lg' }).result.then((result) => {
      console.log(result);
    }, (reason) => {
      console.log(reason);
    });
  }
}

我有点困惑,因为我认为内容用于将参数传递给模态.但是我认为这只是open方法需要找到正确模板的名称?

I'am a little bit confused, because I thought the content is used to pass parameters to the modal. But in my opinion it's only the name the open method needs to find the correct template?

那我该如何传递参数呢?

So how can I pass parameters?

推荐答案

要将参数/数据传递给模态,我的猜测是使用componentInstance:

To pass parameters/data to the modal, my guess would be to use the componentInstance:

open(content) {
    const modal: NgbModalRef = this.modalService.open(content, { size: 'lg' });

    (<MyComponent>model.componentInstance).data = 'hi';

    modal.result.then((result) => {
      console.log(result);
    }, (reason) => {
      console.log(reason);
    });
}

这假定componentInstance的类型为MyComponent,并且它具有公共属性data

This assumes that the componentInstance is of type MyComponent and that it has a public property data

这篇关于如何将参数传递给模态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆