I’m trying to set up a form in an Angular template, as per the example given in
"HTML Input form* Attributes" – w3schools:
<form action="/action_page.php" id="form1">
...
</form>
...
<input type="text" id="lname" name="lname" form="form1">
The important bits are the form="form1"
on the last line and the id="form1"
on the first line, which link the <input>
element to the <form>
element, without having to have the <input>
nested within the <form>
.
When I try to update my Angular HTML template with a hardcoded value, everything works fine:
<input form="my-form-id">
But I need to update it dynamically:
<input [form]="dynamicVariable">
Unfortunately, using the [form]
attribute with square brackets causes the following error:
Can't bind to 'form' since it isn't a known property of 'input'.ngtsc(-998002)
Is there a way to use the form
attribute dynamically in an Angular template?
2
Answers
Instead of
Use
Although I don't think this works for practical Angular usage. For example, if I try:
The first input works fine, but the second input causes an error:
Here , AfterViewInit lifecycle hook is used to set the form property of the input element to the form element using plain JavaScript.
Create your form group as usual in your component class:
Your template will have the form and input element separately: