Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get reference to element inside ng-template

this is my template:

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

I cant get reference to #newimg when its inside ng-template(it works fine if i change ng-template to div!)

@ViewChild('newimg') img: ElementRef; //doesnt work

I need to set its value to empty after image uploaded but its always undefined.

like image 966
Veto Avatar asked Nov 22 '17 07:11

Veto


1 Answers

<ng-template> is not added to the DOM, it exists only in JavaScript code, when the Application runs. Only when it is stamped using for example a structural directive like *ngFor or *ngIf, the content is actually created in the DOM and bindings, components, directives, ... are instantiated.

Therefore without stamping, there won't be any #template to query for.

like image 106
Günter Zöchbauer Avatar answered Oct 01 '22 04:10

Günter Zöchbauer