这是我的模板:
<ng-template #template>
<input #newimg type="file" class="col-md-10" (change)="fileChange($event)"/>
</ng-template>当#newimg在ng-template中时,我无法获得对它的引用(如果我将ng-template更改为div,它可以正常工作!)
@ViewChild('newimg') img: ElementRef; //doesnt work我需要设置它的值为空后,图像上传,但它总是未定义。
发布于 2017-11-22 15:55:04
当应用程序运行时,<ng-template>不会添加到DOM中,它只存在于JavaScript代码中。只有在使用诸如*ngFor或*ngIf之类的结构化指令来标记内容时,才会在DOM和绑定、组件、指令等中实际创建内容。是实例化的。
因此,如果没有标记,就不会有任何要查询的#template。
发布于 2021-08-27 04:51:29
使用ContentChild而不是ViewChild,因为您的模板引用位于投影内容(ngtemplate)中
@ContentChild('newimg') img: ElementRef;https://stackoverflow.com/questions/47428783
复制相似问题