Eugene Sukh
Eugene Sukh

Reputation: 2737

Populate input, when value in input changes (Angular)

I have two inputs, one for Landlord Name, and second for Organization Name

Here is code

  <div class="form-group">
                    <label>{{l("Name")}}</label>
                    <input #nameInput class="form-control" type="text" name="name" [(ngModel)]="landlord.name" required maxlength="32">
                </div>
                <div class="form-group">
                    <label>{{l("OrganizationName")}}</label>
                    <input class="form-control" type="email" name="organizationName" [(ngModel)]="landlord.organizationName" required maxlength="500">
                </div>

I need to populate organizationName, when I enter value in Name field. How I can do this?

Upvotes: 0

Views: 1692

Answers (1)

Borys Kupar
Borys Kupar

Reputation: 1721

You can use ngModelChange event emitter, like:

<input #nameInput (ngModelChange)="onNameChange($event)" class="form-control" type="text" name="name" [(ngModel)]="landlord.name" required maxlength="32">

And define onNameChange in your component:

public onNameChange(name: string) {
    // implement some logic for picking the organization name
    // and update the input
    this.landlord.organizationName = 'some organization name';    
}

Upvotes: 1

Related Questions