1

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>
5
  • did you try <input type="hidden">? Commented Sep 25, 2019 at 2:31
  • yeah, hiding the input isn't the problem, it's getting the value attribute to appear. Commented Sep 25, 2019 at 2:32
  • please show your ts code Commented Sep 25, 2019 at 3:29
  • if you need to just have an attribute to send along the format while submitting why not just add it in your component class why create an input? Commented Sep 25, 2019 at 3:34
  • stackoverflow.com/questions/46478708/… Commented Sep 25, 2019 at 5:18

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.