javascriptangularangularjs-directiveangularjs-ng-form

value attribute does not appear in Angular ngForm


I have a simple Angular ngForm I'm using for a put request. Because I need the id attribute to make the put request, I have attempted to add it in a hidden value. However, whenever the form is submitted the hidden id field is blank.

How can I pass in a pre-defined value to an input that will appear when the form is submitted?

I have tried multiple approaches, but nothing I have found online has worked. The hidden id field value is the one that won't appear.

The form I have is:

    <form #editForm="ngForm" (ngSubmit)="editTodoTest(editForm.value)">
      Todo<input type="text" name="description" ngModel /> UserId<input
        type="text"
        name="userId"
        ngModel
      />
      Completed<input type="text" name="completed" ngModel />
      <input type="hidden" name="id" value="10" ngModel />
      <input type="submit" />
    </form>

Solution

  • try this:

       <form #editForm="ngForm" (ngSubmit)="editTodoTest(editForm.value)">
      Todo<input type="text" name="description" ngModel /> UserId<input
        type="text"
        name="userId"
        ngModel
      />
      Completed<input type="text" name="completed" ngModel />
      <input type="hidden" name="id" value="10" ngModel=10 />
      <input type="submit" />
    </form>
    

    for STACKBLIZ example