Javascript 为什么elementRef的nativeElement未定义?
当转到此方法时,当文本字段的输入侦听器被触发时,它工作正常。但是,单击按钮并转到此处时,容器ElementRef未定义。字段已定义。Javascript 为什么elementRef的nativeElement未定义?,javascript,html,angular,Javascript,Html,Angular,当转到此方法时,当文本字段的输入侦听器被触发时,它工作正常。但是,单击按钮并转到此处时,容器ElementRef未定义。字段已定义。 想知道我在输入监听器中缺少了什么,但在单击监听器中没有。似乎容器元素没有渲染,因为*ngIf,Angular无法引用它,因为它还没有渲染。尝试使用[隐藏]=!如果不希望元素可见,但希望引用有效,则为isFocused。执行this.Container.nativeElement语句时,是否确定isFocused为true? <div class="
想知道我在输入监听器中缺少了什么,但在单击监听器中没有。似乎容器元素没有渲染,因为*ngIf,Angular无法引用它,因为它还没有渲染。尝试使用[隐藏]=!如果不希望元素可见,但希望引用有效,则为isFocused。执行this.Container.nativeElement语句时,是否确定isFocused为true?
<div class="parent">
<div class="input-group">
<input [(ngModel)]="InputValue"
#Field (input)="changedEvent()" (focus)="onFocus()" />
<button (click)="changedEvent()">click</button>
</div>
<div #Container *ngIf="isFocused">
// a table
</div>
</div>
//this is how they have been declared in the component
@ViewChild('Container', { static: false }) Container: ElementRef;
@ViewChild('Field', { static: false }) Field: ElementRef;
//inside the method
const element = this.Field.nativeElement;
const list = this.Container.nativeElement;