【问题标题】:ElementRef of ng-bootstrap modalng-bootstrap modal 的 ElementRef
【发布时间】:2017-06-11 17:52:57
【问题描述】:

我想从 Angular 2 的组件中引用 ng-bootstrap 模态内部的 html 元素

<ng-template #liveView id="liveView" let-c="close" let-d="dismiss">
  <div class="modal-header">
    <h4 class="modal-title">Live View</h4>
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <canvas #liveViewCanvas style="border: 1px solid black;" width="270" height="480"></canvas>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-danger btn-xs" (click)="closeLiveViewModal()">
      <span class="fa fa-trash"></span> Close
    </button>
  </div>
</ng-template>

如何在 Angular 2 组件中引用 #liveViewCanvas?通过@viewchild 引用时,我收到“ERROR TypeError: Cannot read property 'nativeElement' of undefined

【问题讨论】:

  • 你查到这件事的真相了吗?我也遇到了同样的问题

标签: angular


【解决方案1】:

也许是这样的。

const modalRef = this.modalService.open(HoldingModalsProductCreateComponent,{backdrop: 'static'});
modalRef._windowCmptRef.hostView.rootNodes[0];

【讨论】:

    【解决方案2】:

    和 NgIf 指令一样,你只能在 DOM 中使用引用。如果你想操作模板,只需用模态的模板和逻辑创建一个组件,然后启动它。

    export class NgbdModalComponent {
      constructor(private modalService: NgbModal) {}
    
      open() {
        const modalRef = this.modalService.open(NgbdModalContent);
        modalRef.componentInstance.name = 'World';
      }
    }
    

    【讨论】:

    • 我遇到了和 OP 一样的问题。我已经尝试了上述方法,但 componentInstance 未定义(尽管 modalRef 中有其他信息)。
    • 弄清楚为什么我无法获得组件实例;我正在打开内容(我无法更改,因此此方法对我不起作用)。
    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 2013-03-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2021-07-27
    • 2017-05-28
    相关资源
    最近更新 更多