Angular 在角度2/4中编辑时重用form value对象
我有一个模板驱动的表单,如下所示:Angular 在角度2/4中编辑时重用form value对象,angular,angular-forms,Angular,Angular Forms,我有一个模板驱动的表单,如下所示: <form #f="ngForm" (submit)="onSubmit(f.value)"> <input type="text" name="name" [ngModel]="person.name"> <input type="text" name="ssn" [ngModel]="person.ssn"> <button type="submit">Submit</button>
<form #f="ngForm" (submit)="onSubmit(f.value)">
<input type="text" name="name" [ngModel]="person.name">
<input type="text" name="ssn" [ngModel]="person.ssn">
<button type="submit">Submit</button>
</form>
{ name: 'John', ssn: '1234567' }
它按原样工作,但我希望我的表单模板不那么冗长。我希望避免在编辑时将每个输入绑定到表单值对象的相应属性。是否可以将整个对象绑定到表单值
我希望能够将先前从f.value
检索到的对象绑定到表单,但无法
我想这应该行得通,但是不行:
<form #f="ngForm" (submit)="onSubmit(f.value)" [ngModel]="person">
<input type="text" name="name" ngModel>
<input type="text" name="ssn" ngModel>
<button type="submit">Submit</button>
</form>
提交
这也不起作用:
<form #f="ngForm" (submit)="onSubmit(f.value)" [value]="person">
<input type="text" name="name" ngModel>
<input type="text" name="ssn" ngModel>
<button type="submit">Submit</button>
</form>
提交
实现灵活优雅表单模板的最佳方法是什么?要实现这一点,您应该使用模板驱动的表单,而不是使用模板驱动的表单。@cyrix是不是反应式表单具有内置功能?默认的一个lostfocus?要实现这一点,您应该使用而不是模板驱动的表单。@cyrix是不是被动表单具有内置功能?默认一个lostfocus?