我正在 Angular 中构建一个反应式表单,上面有 3 个提交按钮。
<form [formGroup]="sessionForm" (submit)="submitSession($event)">
<div class="row">
<label>
<span>Title</span>
<input type="text" formControlName="title" [class.required]="markRequired.indexOf('title') >= 0">
</label>
</div>
... more fields ...
<div *ngIf="!admin" class="row">
<button type="submit" (click)="setBtnClicked('submit')" class="btn btn-primary">Submit</button>
</div>
<div *ngIf="admin" class="row">
<button type="submit" (click)="setBtnClicked('save')" class="btn btn-primary">Save</button>
<button type="submit" (click)="setBtnClicked('approve')" class="btn btn-success">Save and approve</button>
<button type="submit" (click)="delete($event, false)" [hidden]="confirmDelete" class="btn btn-danger">Delete</button>
<button type="submit" (click)="delete($event, true)" [hidden]="!confirmDelete" class="btn btn-danger">Are you sure?</button>
</div>
<div [hidden]="!showSuccess" class="msgBox msgBox-success">Your session has been submitted! It will need to be approved before it is listed.</div>
</form>
在每个按钮上,我附加了一个(click)
处理程序,该处理程序触发一个函数来跟踪哪个按钮被点击:
setBtnClicked(value) {
this.btnClicked = value;
}
但是,当有人点击输入时,我注意到 的值this.btnClicked
等于表单中的第一个按钮。
我不确定如何跟踪用户何时点击返回或何时点击按钮,因此我可以做出不同的响应,或者我的结构是否错误。我想submitSession
在任何提交时触发该功能,无论是返回还是按钮。我确实尝试将 formControl 添加到按钮,但失败了。