好吧,也许这还不清楚。获取此表格:
<form (ngSubmit)="submit()" #crisisForm="ngForm">
<input type="text" name="name" [(ngModel)]="crisis.name">
<button type="submit">Submit</button>
<button type="button" (click)="preview()">Preview</button>
<button type="reset" (click)="reset()">Reset</button>
</form>
为什么所有按钮都会触发submit()
功能 ?以及如何避免这种情况?
我看到有两种解决方案:
1) 指定类型=“按钮”明确地(我认为这是更优选):
<button type="button" (click)="preview();">Preview</button>
根据W3规范:
-
http://w3c.github.io/html-reference/button.html http://w3c.github.io/html-reference/button.html
A button element with
no type attribute specified represents the same thing as a button
element with its type attribute set to "submit".
2) Use $event.preventDefault()
:
<button (click)="preview(); $event.preventDefault()">Preview</button>
or
<button (click)="preview($event);">Preview</button>
preview(e){
e.preventDefault();
console.log('preview')
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)