简体   繁体   English

根据 Angular 中的 function 值禁用反应形式的单选按钮

[英]Disable radio buttons in reactive form based on function value in Angular

I have an input made up of radio buttons, but I want to disable some of them based on a function value (which depends on the component's input).我有一个由单选按钮组成的输入,但我想根据 function 值(取决于组件的输入)禁用其中的一些。 If I try to do this, I get a warning about using disabled attribute in a reactive form.如果我尝试这样做,我会收到有关在反应形式中使用disabled属性的警告。

<div *ngFor="let option of options">
  <p-radioButton [inputId]="option.id" name="option" [value]="option" formControlName="option" [disabled]="foo(option)"></p-radioButton>
  <label [for]="option.id">{{ option.name }}</label>
</div>

Using the warning's suggestion, I am supposed to use a FormControl and set its disabled to true/false, but I'm not sure I understand how I would be able to do this with my radio buttons.使用警告的建议,我应该使用 FormControl 并将其disabled设置为 true/false,但我不确定我是否理解如何使用单选按钮执行此操作。

Maybe you can try [attr.disabled]="foo(option)" .也许您可以尝试[attr.disabled]="foo(option)"

Also, if you prefer changing at the typescript code, there is a discussion about Disable Input fields in reactive form that I think maybe can help you.此外,如果您更喜欢更改 typescript 代码,有一个关于禁用反应形式的输入字段的讨论,我认为这可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM