modal-body中的input type =“text”未定义

问题描述 投票:3回答:2

我试图从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

我该如何解决?

谢谢。

angular bootstrap-modal ngx-bootstrap
2个回答
3
投票

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

HTML

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

TS

let id = this.workerId;

2
投票

 @ViewChild('workerId') workerId: ElementRef;

在构造函数之前。那么它会像你期望的那样工作

© www.soinside.com 2019 - 2024. All rights reserved.