Two way binding using "ngModel"

When I use ngModel to bind variables in two elements in a single component, then from component to DOM only updates the second variable as the value for both elements.

For example:
<input [(ngModel)]=“userID” />
<input [(ngModel)]=“password” />

On the component side:
userID = “username”;
password = “password”;

In this case both the input field displays “password”. Why isn’t the userID field in the input displaying “username”.

2 Likes

Dont know if you already solved the issue. I did the same as you and it worked just fine, I put the Id’s but its works even without them:

courses.component.ts

import { Component } from ‘@angular/core’;

@Component({
selector:‘courses’,
template: <input id="userIdInput" [(ngModel)]="userID" /> <input id="passwordInput" [(ngModel)]="password" />
})

export class CoursesComponent {
password:string = ‘Change@123’;
userID:string =‘Admin’;
}

1 Like