如何将表单action属性与变量绑定?Angular表单绑定可行性咨询
action Attribute of an Angular Form to a Variable? Absolutely, this is totally feasible in Angular—let me walk you through how to do it properly!
First, let's look at your current code: you're using interpolation ({{url}}) for the action attribute, which works, but Angular's preferred approach for binding properties to variables is using property binding syntax ([action]="url"). Both methods achieve the same end result, but property binding aligns better with Angular's data flow patterns and keeps your code consistent with how you're already binding formGroup with [formGroup].
Here's the cleaned-up, corrected version of your form code using property binding for action:
<form [formGroup]="onlinepaymentForm" [action]="url" target="_blank" method="POST" #form> <!-- Your form controls go here --> </form>
A couple of quick tips to avoid issues:
- If your
urlvariable might benullorundefinedat any point (like before data loads), use the safe navigation operator to prevent potential runtime errors:[action]="url?" - The
paddingin your original code looks like a typo—you'll want to remove that, or apply padding via a CSS class if needed.
To break down why this works: Angular supports binding to both native HTML attributes and DOM properties. For the action attribute specifically, using [action] directly binds your component's url variable to the form element's DOM action property. This means if url updates in your component later, the form's action will automatically reflect that change without extra work.
内容的提问来源于stack exchange,提问作者user7523193

