在Angular component中,我试图创建一个新的input
html元素,并将其插入模板的特定位置
组件代码
@ViewChild('pickerLocation', { static: false }) pLocationSpan: ElementRef<HTMLSpanElement>;
construc到r(
private elementRef: ElementRef,
private renderer: Renderer2,
@Inject(DOCUMENT) private document: Document) { }
ngOnInit(): void {
const pickerInput = this.renderer.createElement('INPUT');
const picker = new TempusDominus(pickerInput);
this.renderer.appendChild(this.pLocationSpan.nativeElement, pickerInput);
}
模板代码
<div class="col-sm-6">
<label for="datetimepickerInput">{{label}}</label>
<span id="locationSpan" ngModel #pickerLocation></span>
<span class="input-group-text">
<span class="fa-solid fa-calendar"></span>
</span>
</div>
但是如果我改变了路由
this.renderer.appendChild(this.pLocationSpan.nativeElement, pickerInput);
到
this.renderer.appendChild(this.elementRef.nativeElement, pickerInput);
它可以工作,但是我需要在特定位置输入.那么问题出在哪里呢??