【问题标题】:How to disable Angular inputs on toggle如何在切换时禁用 Angular 输入
【发布时间】:2023-02-10 16:24:34
【问题描述】:

我正在做内联编辑。我希望输入被禁用,直到用户单击编辑按钮,与保存按钮相对。我试图像这样设置它[disabled]="!hidemeSub[index]",但它不起作用。我究竟做错了什么?

.html

  <div *ngFor="let item of items; let index = index">
    <form [formGroup]="rates">
      <div  formGroupName="type">
        <div [hidden]="hidemeSub[index]"
          (click)="hidemeSub[index] = !hidemeSub[index];">Edit</div>
        <div (click)="editRates(item.id, index)" [hidden]="!hidemeSub[index]">Done</div>
        <div formArrayName="options">
          <ng-container [formGroupName]="index">
              <input type="text" class="form-control" [disabled]="!hidemeSub[index]" formControlName="rateRangeFrom">
              <input type="text" class="form-control" [disabled]="!hidemeSub[index]" formControlName="rateRangeTo">
          </ng-container>
        </div>
      </div>
    </form>
  </div>

.ts

hidemeSub = {};

【问题讨论】:

  • 你有错误吗?此外,hidemeSub 是一个对象,您不能使用索引对其进行迭代。所以,请提供更多来自 ts 的代码。

标签: angular


【解决方案1】:

你需要使用[属性.禁用]代替[禁用]

<input type="text" class="form-control" [attr.disabled]="!hidemeSub[index]" formControlName="rateRangeFrom">

传递的值应该保持真或空

简单的工作示例 https://stackblitz.com/edit/angular-ivy-87nxyh?file=src/app/app.component.html

【讨论】:

  • 你好!感谢你的回答!是的,但现在输入总是被禁用,它不会切换
  • @aelin_galathius 我在答案中添加了一个工作示例。可以参考一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 2022-11-27
  • 2017-12-21
  • 2019-05-04
相关资源
最近更新 更多