1
如果我的fileInput元素是在一個正常的div,那麼它工作正常。但是,如果我把它放在<ng-template></ng-template>
內,那麼我就會得到undefined。Angular ViewChild fileInput註解未定義
這裏是我的代碼:
HTML
<ng-template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Modal title</h4>
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')"></button>
</div>
<div class="modal-body">
<form #documentsForm="ngForm" (ngSubmit)="onEditSubscriberDocumentsSubmit(documentsForm.value);documentsForm.reset()">
<input #fileInput type="file" #select name="document" [(ngModel)]="document" (click)="onDocUpload()" />
<input type="submit" value="Save" class="btn btn-success" [hidden]="addDocHidden">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="c('Close click')">Close</button>
</div>
</ng-template>
TS
import { Component, OnInit, ViewChild , ElementRef} from '@angular/core';
export class EditSubscriberComponent {
constructor(private http: Http, private route: ActivatedRoute, private router: Router, private modalService: NgbModal) { }
@ViewChild("fileInput") fileInput: ElementRef;
ngOnInit() {
console.log(this.fileInput)
// This is undefined. If I place the <input #fileInput type="file">
// in the main div,not under ng-template, then I am getting the
// desired output
}
}
請建議我該怎麼交流從納克模板塞斯ViewChild
感謝您的指導。你能不能解釋這行'this.vcRef.createEmbeddedView(this.template)'?我在Angular中很新,使用定居者並沒有幫助。 –
您正在使用哪個角庫?應該在你的模態打開時調用Setter – yurzui
我正在使用NgbModal模態 –