输入类型="text" inside modal-body is undefined

input type="text" inside modal-body is undifiend

我正在尝试从 modal-body 中的输入中获取值,但对象是未定义的。

我有以下代码:

在.html:

<ng-template #myModal>
    <div class="modal-header">
        <h1>Title</h1>
    </div>
    <div class="modal-body" style="overflow-warp: break-word;">
        <p>
            Enter ID: <input autofocus type="text" #workerId>
        </p>
    </div>
    <div class="modal-footer">
        <button type="submit" (click)="Confirm()" label="Submit"></button>
    </div>
</ng-template>

在 .ts 中:

let id = this.workerId.nativeElement.value;//Undifiend

我该如何解决?

谢谢。

尝试使用 [(ngModel)] 绑定来获取文本框的值。

HTML

<input autofocus type="text" [(ngModel)]="workerId">

TS

let id = this.workerId;

添加

 @ViewChild('workerId') workerId: ElementRef;

在构造函数之前。然后它将按您的预期工作