Is a name attribute mandatory for form elements when using Angular?

I use Angular JS to manage all forms. Data for inputs is stored in the corresponding one ngModel, which can be processed in $scope controller.

So, I have the following form settings:

<form name="addJob" novalidate data-ng-submit="addJob.$valid && addJob(job)">
  <input type="text" placeholder="Job Title" data-ng-model="job.title" required />
  <textarea placeholder="Brief" data-ng-model="job.brief"></textarea>
  <button type="submit" data-ng-disabled="addJob.$invalid">Add Job</button>
</form>

This works absolutely fine in all major browsers (except that I have not tested IE). You will notice that I did not include the name attributes on the input or text field. Do I need them for any reason? I read the following:

Note: Only form elements with a name attribute will have their values passed when submitting a form. 

But my data is transmitted absolutely accurately, because it is connected with ngModel. Was the correct method to include or not to include name attributes?

+4
2

name ng-, formController , . , ( ), name. . .

ngModelController , $name, , .

ngModelController source

this.$name = $attr.name; 

ng-model $addControl ( ), name formController, , , angular .

FormController

form.$addControl = function(control) {
    // Breaking change - before, inputs whose name was "hasOwnProperty" were quietly ignored
    // and not added to the scope.  Now we throw an error.
    assertNotHasOwnProperty(control.$name, 'input');
    controls.push(control);

    if (control.$name) {
      form[control.$name] = control;
    }

, name, angular .

+5

, , , ng- . , , name ui. angularjs api : https://docs.angularjs.org/api/ng/directive/input. , , .

: , name . , Angular, ng-Model, .

+4

Source: https://habr.com/ru/post/1570514/


All Articles