【问题标题】:Unable to get textbox value in the controller when using stepper Angular4使用步进器Angular4时无法在控制器中获取文本框值
【发布时间】:2018-04-17 10:18:58
【问题描述】:
<form #subleaseForm01="ngForm">
    <ng-template mdStepLabel>Fill out initial details</ng-template>
    {{diagnostic}}
    <md-grid-list cols="1" rowHeight="8:1">
        <md-grid-tile>
            <label for="title">Title</label>
            <md-form-field>
                <input mdInput type="text" id="title1" placeHolder="Title" class="form-control" required ngModel name="title" #name="ngModel">                TODO: remove this: {{model.title}}
            </md-form-field>
            <br>
            <div [hidden]="name.valid || name.pristine">
                Name is required
            </div>
        </md-grid-tile>
    </md-grid-list>
    <div>
        <button md-raised-button color="warn" mdStepperNext>Next</button>
    </div>
</form>

我想获取在标题字段中输入的值。我得到的只是未定义的。我正在使用 ngModel 标签来绑定变量。我编写了一个定义所需字段的类,并试图记录在提交当前值后创建的新对象。

【问题讨论】:

    标签: angular data-binding angular-material angular4-forms


    【解决方案1】:

    您可以使用[(ngModel)]=""属性获取输入值:

    component.html:

    <input mdInput type="text" id="title1" placeHolder="Title" class="form-control" required [(ngModel)]="title" name="title">
    

    component.ts:

    private title: string;
    

    使用 this.title 访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多