开发者问题收集

如何禁用 angular2 中组件的按钮?

2017-02-27
10407

如何从组件属性中禁用模板中的按钮?假设我有一个这样的组件

    export class PolicyAddComponent {
      ToggleButton: boolean = true;

      SubmitPolicy(value: any) {
        ToggleButton = false;
      }
    }

并且我有一个这样的模板

 <form role="form" [formGroup]="PolicyForm" (ngSubmit)="SubmitPolicy(PolicyForm.value)">
   <input type="date" class="form-control" formControlName="RateDate" placeholder="Rate Date">
     <button type="submit" class="btn btn-primary">Submit</button>
     <button type="submit" class="btn btn-primary" (click)="ShowIssuedPolicy()">Show Issue policy</button>
 </form>

我希望在单击提交按钮后禁用提交按钮并启用显示问题策略按钮。我该怎么做?

2个回答

实现此目标的方法之一是实现一个 boolean 变量,该变量将保持 true 值, 直到 用户点击并查看 Issue policy 。然后,变量设置为 false 并启用提交按钮。

<button type="submit" class="btn btn-primary" [disabled]='checkPolicy'>Submit</button>
<button type="button" class="btn btn-primary" (click)="ShowIssuedPolicy()">Show Issue policy</button>


checkPolicy: boolean = true;

ShowIssuedPolicy(){
  this.checkPolicy = false;
}

Plunker 链接

kind user
2017-02-27

您可以使用以下内容执行此操作:

组件
export class PolicyAddComponent{
    ToggleButton: boolean = true;
    SubmitPolicy(value: any) {
        ToggleButton=false;
    }
}
模板
<form role="form" [formGroup]="PolicyForm" (ngSubmit)="SubmitPolicy(PolicyForm.value)">
    <input type="date" class="form-control" formControlName="RateDate" placeholder="Rate Date">
    <button type="submit" class="btn btn-primary" (click)="SubmitPolicy()">Submit</button>
    <button type="submit" class="btn btn-primary" (click)="ShowIssuedPolicy()" [disabled]="ToggleButton">Show Issue policy</button>
</form>
Teddy Sterne
2017-02-27