Search code examples
angulardata-bindingangular-materialangular4-forms

Unable to get textbox value in the controller when using stepper Angular4


<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>

I want to get the value entered in the title field. All I get is undefined. I am using the ngModel tag to bind the variable. I wrote a class defining the required fields and am trying to log the new object created after the current values are submitted.


Solution

  • You can get the input value using [(ngModel)]="" property:

    component.html:

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

    component.ts:

    private title: string;
    

    Access it w/ this.title.