我试图通过按下父组件上的按钮来执行子组件的功能,但由于某种原因它未定义。
家长:
.. com1.html
<ng-template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body" style="display: flex; justify-content: center;">
<app-com2 [var1]="value" #traceRef></app-com2>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="savePNG()"><i class="fa fa-download"></i>PNG</button>
<button type="button" class="btn btn-outline-dark" (click)="c()">Close</button>
</div>
</ng-template>
<button class="btn btn-sm btn-secondary" (click)="open(content)">Button</button>
...com1.ts
export class com1 implements OnInit {
@ViewChild('traceRef') traceRef;
value = "something";
constructor(private modalService: NgbModal) {}
savePNG() {
this.traceRef.savePNG();
}
open(content: any) {
this.modalService.open(content, { windowClass: 'temp-modal' });
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出我正确的方向,因为其他线程没有帮助:(即使使用 ngAfterViewInit 它仍然未定义。
它不像在常见情况下那样工作的原因ng-template是该NgbModal服务创建模板并且不将其附加到父视图而是附加到根ApplicationRef
const viewRef = content.createEmbeddedView(context);
this._applicationRef.attachView(viewRef);
Run Code Online (Sandbox Code Playgroud)
这意味着您的查询对于您的组件将始终是不脏的。
在通常情况下(参见例如由@ConnorsFan提供),我们使用vcRef.createEmbeddedView它负责检查父视图的查询:
vcRef.createEmbeddedView
||
\/
function attachEmbeddedView(
...
Services.dirtyParentQueries(view); <========== set dirty for queries
Run Code Online (Sandbox Code Playgroud)
我在这里看到的简单解决方案是直接在模板中传递或调用引用:
(click)="traceRef.savePNG()"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3649 次 |
| 最近记录: |