Home > Mobile >  Attribute "name" - How to properly name this attribute?
Attribute "name" - How to properly name this attribute?

Time:09-06

The name attribute is obligatory in the ngModel. However, I don't know what name I should give to this attribute?

By default, I always indicate one.

angular

<label>Nombre 1</label>
<input type="number" name="one" [(ngModel)]="number_one" /> {{ number_one }}
<br /><br />
<label>Number 2</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;
  }
}

I don't have to indicate the same value as there is on the ngModel?

For example:

name="number_one" [(ngModel)]="number_one"

Instead of:

name="one" [(ngModel)]="number_one" ?

Is it correct to do this?

<label>Nombre 1</label>
<input type="number" name="number_one" [(ngModel)]="number_one" /> {{ number_one }}
<br /><br />
<label>Number 2</label>
<input type="number" name="number_two" [(ngModel)]="number_two" /> {{ number_two }}
<br /><br />
<input type="button" value="Addition" (click)="addition()" /> {{ total }}

CodePudding user response:

The name attribute specifies the name of an element.

The name attribute is used to reference elements in a JavaScript, or to reference form data after a form is submitted.

The name attribute is probably the most important attribute of the element. It isn’t strictly required for validation, but you should never omit it. When a form is submitted to the server, the data from the form is included in an HTTP request. The data is packaged as a series of name-value pairs. The name for each name-value pair is the name attribute of each input, and the value is the user-entered (or pre-specified) value. Without the name attribute, an element cannot provide its value to the server on form submission.

  • Related