When using the ngModel
, the name
attribute is required. But how do I choose the right name for this attribute?
Usually, I just go with one
by default.
angular
<label>First Name</label>
<input type="number" name="one" [(ngModel)]="number_one" /> {{ number_one }}
<br /><br />
<label>Second Number</label>
<input type="number" name="one" [(ngModel)]="number_two" /> {{ number_two }}
<br /><br />
<input type="button" value="Addition" (click)="addition()" /> {{ total }}
typescript
export class AppComponent {
name = 'Angular ' + VERSION.major;
number_one: number = 0;
number_two: number = 0;
total: number = 0;
addition(): void {
this.total = this.number_one + this.number_two;
}
}
Do I really need to use the same name as in the ngModel
?
For instance:
name="number_one" [(ngModel)]="number_one"
Instead of:
name="one" [(ngModel)]="number_one"
?
Is it acceptable to do so?
<label>First Name</label>
<input type="number" name="number_one" [(ngModel)]="number_one" /> {{ number_one }}
<br /><br />
<label>Second Number</label>
<input type="number" name="number_two" [(ngModel)]="number_two" /> {{ number_two }}
<br /><br />
<input type="button" value="Addition" (click)="addition()" /> {{ total }}