在Angular 2中禁用按钮

时间:2017-05-15 11:31:33

标签: angular button input components

我想如果输入'合约类型'为空,则按钮'保存'不可点击

保存按钮:

<div class="col-md-4">
        <cic-textbox [control]="formGroup.get('contractType')"></cic-textbox>
      </div>

所有按钮:

 <div class="cic-header-actions pull-left" *ngIf="actions && actions.length > 
 0">
      <button class="btn btn-{{action.style}} m-l-xs" *ngFor="let action of actions" ng-disabled="!contractTypeValid" (click)="execute(action)">
          <cic-icon [icon]="action.icon"></cic-icon>
          {{action.text }}
        </button>
    </div>

定义contractType:

 let contractType: DataDictionaryPropertyExtended = {
            Binding: 'VART:BEZEICHNUNG',
            Label: 'Vertragsart',
            LabelCols: 4,
            ContentCols: 8,
            IsDisabled: this.isDisabled,
            ValidationProperties: [
                <ValidationProperty>{
                    Type: ValidationType.IsNotEmpty,
                    ErrorMessage: 'Vertragsart darf nicht leer sein.',
                }
            ]
        };

BUTTON SAVE GREEN:

enter image description here

2 个答案:

答案 0 :(得分:52)

ng-disabled="!contractTypeValid"更改为[disabled]="!contractTypeValid"

答案 1 :(得分:0)

我尝试使用[disabled]="!editmode",但不适用于我的情况。

这是我的解决方案[disabled]="!editmode ? 'disabled': null",我与谁分享关注的地方。

<button [disabled]="!editmode ? 'disabled': ''" 
    (click)='loadChart()'>
            <div class="btn-primary">Load Chart</div>
    </button>

Stackbliz https://stackblitz.com/edit/angular-af55ep