【问题标题】:Enable/Disable specific buttons in a ngFor启用/禁用 ngFor 中的特定按钮
【发布时间】:2020-02-15 18:15:53
【问题描述】:

我想在特定行中启用保存按钮,并通过编辑按钮在上面的循环中禁用两个按钮(添加选择器和添加部分)。但我无法仅禁用/启用生成的特定按钮。 我也想在我的组件中管理它以启用/禁用。 我希望任何人都可以帮助我。

我的表单看起来像这样 -> https://gyazo.com/329e442becd93222d33c15b1232cc9d2

<div formArrayName="sectionList">
    <div *ngFor="let section of myForm.get('sectionList').controls; let i=index" [formGroupName]="i">
      <div formGroupName="section">
        <button mat-button color="primary" (click)="enableSection(i)">Edit</button>    
        <mat-form-field>
          <input matInput placeholder="name" formControlName="name">
          <mat-error *ngIf=""> 
              todo
          </mat-error>
        </mat-form-field>   
        <button mat-raised-button color="warn" (click)="deleteSection(i)" >Delete</button>    
        <button mat-raised-button color="primary" [disabled]="true" (click)="saveSection(i)">Save</button> 
      </div>

      <div formArrayName="selectorList">
        <div *ngFor="let selector of section.controls.selectorList.controls; let j=index" [formGroupName]="j">
          <button mat-button color="primary" (click)="enableSelector(i, j)">Edit</button>    
          <mat-form-field>
            <input matInput placeholder="locator" formControlName="locator" >
          </mat-form-field>
          <mat-form-field>
            <input matInput placeholder="language" formControlName="language" >
          </mat-form-field>            
          <mat-form-field>
            <input matInput placeholder="value" formControlName="value" >
          </mat-form-field> 
          <button mat-raised-button color="warn" (click)="deleteSelector(i,j)">Delete</button> 
          <button mat-raised-button color="primary" [disabled]="isDisabled" (click)="saveSelector(i,j)">Save</button>   
        </div>
        <button mat-raised-button color="primary" (click)="addSelector(i)">Add Selector</button>    
      </div>    
    </div> 
    <button mat-raised-button color="primary" (click)="addSection()">Add Section</button>     
  </div>
 enableSection(i){
    this.sectionForms.controls[i].get('section').enable();
  }

  enableSelector(i, j){
    this.isDisabled = false;
    this.myForm.get('sectionList.' + i + '.selectorList.' + j).enable();
  }

【问题讨论】:

  • 你得到什么错误?
  • 我没有收到错误,我只是不知道如何禁用/启用特定按钮。
  • 你能提供一个minimal reproducible example吗?

标签: angular typescript


【解决方案1】:

只需添加到您要禁用的按钮[disabled]="rowIWannaDisable === i" 其中i*ngFor 的索引,如ngFor="let section of myForm.get('sectionList').controls; let i=index"rowIWannaDisable 是您希望禁用按钮的行号。您也可以使用函数而不是 rowIWannaDisable 来动态选择禁用的行。

【讨论】:

  • 感谢@alexortizl 的回答。我试过这个:&lt;button mat-raised-button color="primary" [disabled]="sectionI === i &amp;&amp; selectorJ === j" (click)="saveSelector(i,j)"&gt;Save&lt;/button&gt;,如果我按下编辑,它会将按钮设置为“禁用”。但我需要先禁用所有保存按钮,然后使用编辑 btn 启用一个保存 btn。你有什么想法?遗憾的是我没有找到 [启用] 属性,我只需要相反的效果。
  • 按钮默认启用。如果[disabled] 属性的条件为真,则按钮将被禁用,如果为假,它将被启用,这就是没有enabled 属性的原因。例如,在您上面的评论中,当sectionI === i &amp;&amp; selectorJ === j 返回 true 时,按钮将被禁用,如果返回 false,则按钮将被启用。此处的解决方案是在 disabled 属性中实现符合您要求的逻辑。
猜你喜欢
  • 2021-08-07
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 2017-10-05
  • 2019-04-16
  • 2021-11-08
  • 1970-01-01
  • 2013-09-11
相关资源
最近更新 更多