ngSubmit
說明
- 取代原本html中
<form>的submit機制 - 搭配
type="submit"按鈕與(ngSubmit)事件
template語法:
<form (ngSubmit)="onSubmit(myForm)" #myForm="ngForm">
...
<button type="submit">Submit</button>
</form>
程式碼範例:
1.將template-driven-form.component.html中的送出表單按鈕加入type="submit"
<button type="submit">送出表單</button>
2.在template-driven-form.component.ts中寫送出表單的方法
submitForm(form){
console.log("submit!");
console.log(form);
}
3.在template-driven-form.component.html的<form>tag中加入(ngSubmit),並掛上submitform方法
<form (ngSubmit)="submitForm(myform)" #myform="ngForm">
4.測試送出表單按鈕,並無作用
5.在template-driven-form.component.html中,將送出表單按鈕放入formtag下,測試送出表單按鈕