获取ng-template中的元素参考

Vet*_*eto 6 ngx-bootstrap angular ngx-bootstrap-modal angular5

这是我的模板:

<ng-template #template>
    <input #newimg  type="file" class="col-md-10" (change)="fileChange($event)"/>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

我不能得到引用#newimg时,其内部的ng-template(如果我改变它工作正常ng-templatediv!)

@ViewChild('newimg') img: ElementRef; //doesnt work
Run Code Online (Sandbox Code Playgroud)

我需要在上传图像后将其值设置为空,但始终未定义.

Gün*_*uer 4

<ng-template>不添加到 DOM,它仅在应用程序运行时存在于 JavaScript 代码中。仅当使用例如*ngFor或 之类的结构指令对其进行标记时*ngIf,内容才会在 DOM 中实际创建,并且绑定、组件、指令......都会被实例化。

因此,如果没有盖章,就没有任何#template可查询的地方。