angular - 如何在 Angular 5 应用程序中禁用单选按钮
问题描述
我需要通过另一个使用反应式表单/表单组动态生成的外部复选框单击事件来禁用单选按钮。
我的组件代码适用于文本框和下拉列表,但不适用于单选框或复选框
这是生成的代码
<label _ngcontent-c6="" class="container-vertical"> Dog
<input _ngcontent-c6="" type="radio" ng-reflect-form-control-name="01cb437e-0015-4c45-9828-fb2d1d" ng-reflect-value="8f77b6e1-2495-4781-9e40-520e94" ng-reflect-name="01cb437e-0015-4c45-9828-fb2d1d" class="ng-untouched ng-pristine ng-invalid">
<label _ngcontent-c6="" class="container-vertical"> Cat
<input _ngcontent-c6="" type="radio" ng-reflect-form-control-name="01cb437e-0015-4c45-9828-fb2d1d" ng-reflect-value="4102b66c-b8c3-4d41-afcd-5852e4" ng-reflect-name="01cb437e-0015-4c45-9828-fb2d1d" class="ng-untouched ng-pristine ng-invalid">
<label _ngcontent-c6="" class="container-vertical"> Fish
<input _ngcontent-c6="" type="radio" ng-reflect-form-control-name="01cb437e-0015-4c45-9828-fb2d1d" ng-reflect-value="5dc08818-38b0-4563-8e0a-ce8901" ng-reflect-name="01cb437e-0015-4c45-9828-fb2d1d" class="ng-untouched ng-pristine ng-invalid">
模板
<div *ngSwitchCase="'radio'">
<div *ngFor="let opt of question.options" > <small>radio</small>
<label class="container-vertical"> {{opt.value}}
<input type="radio"
[formControlName]="question.key"
[value]="opt.key"
[(ngModel)]="question.value"
(change)="onChangeValue(question.value, previousValue, responseId, question.key, 'radio'); previousValue = question.value"
(focus)="previousValue=question.value"
>
<span class="checkmark"></span>
</label>
</div>
</div>
零件
private handleQuestionAnswerProvidedStateChange(questionAnswerProvidedState:QuestionAnswerProvidedStateDataModel)
{
var formControlNameReference = questionAnswerProvidedState.QuestionId.substring(1,30);
// ???????????? need help here ... how to get reference of radio button to disable it
if(questionAnswerProvidedState!=null)
{
var elementReference = <HTMLInputElement> document.getElementById(questionAnswerProvidedState.QuestionId);
if(questionAnswerProvidedState.AnswerNotProvided == true)
{
elementReference.disabled = true;
elementReference.value = "";
elementReference.classList.add("disableInput");
}
else if(questionAnswerProvidedState.AnswerNotProvided == false)
{
elementReference.disabled = false;
elementReference.classList.remove("disableInput");
}
}
}
解决方案
<input type="radio" [(ngModel)]="radioButtonVisibility" [attr.disabled]="flag ? '' : null"
formControlName="radioButtonVisibility"/>
使用上述属性[attr.disabled]
。如果变量标志包含任何值,则单选按钮将被禁用。如果变量标志是,null
那么单选按钮将被启用。标志值必须根据要求设置为空或非空。
推荐阅读
- excel - 下面的代码只填写了 excel 工作表的一页。上面的颜色将填充到从 A 到 Q 的行中
- angular - Angular 提供者被具有相同提供者的父模块覆盖
- amazon-web-services - terraform "element" 和 "concat" 一起使用
- react-native - 当我拖动以打开侧面菜单时,React DrawerNavigation 不工作(没有任何反应)
- azure - 使用 terraform 部署 Azure PowerShell 函数应用时的问题
- google-cloud-firestore - Flutter webapp:Snapshort返回一个空值
- java - 日期之间差异的平均值
- h2o - H2O autoML 在默认时间预算后不会停止
- kubernetes - 访问多个集群的参数
- ios - 在 iOS 上启动 BlueJeans 应用程序的 URL 方案?