首页 > 解决方案 > 在同一个按钮上保存和更新?

问题描述

如何从同一个按钮调用保存和提交?

    <button type="submit" class="btn btn-sm btn-brand"
        [disabled]="!frmAgrTemp.valid"  (click)="SubmitInfo()">
        Save
    </button>
    <button type="submit" class="btn btn-sm btn-brand"
        [disabled]="!frmAgrTemp.valid"  (click)="UpdateInfo()">
       Update
    </button>

标签: angular

解决方案


一般来说,您可以有一个变量来指示何时是新的注册,例如

newReg:boolean=true

//at very first, e.g. in ngOnInit if params get a parameter you can make
this.newReg=false

那么你有两个选择:

1.-两个带有 *ngIf 的按钮来显示一个或另一个

<button type="submit" *ngIf="newReg" ...>Save</button>
<button type="submit" *ngIf="!newReg" ...>Update</button>

2.-更改标题的独特按钮和保存或更新注册表的独特功能

<button type="submit" (click)="Submit()" >{{newReg?'Save':'Update'}}</button>

Submit()
{
     if (this.newReg){
          ..save the reg..
     }
     else{
          ..update the reg..
     }
}

推荐阅读