【发布时间】:2018-11-04 20:01:31
【问题描述】:
我需要通过另一个使用响应式表单/表单组动态生成的外部复选框单击事件来禁用单选按钮。
我的组件代码适用于文本框和下拉菜单,但不适用于单选框或复选框
这是生成的代码
<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");
}
}
}
【问题讨论】:
-
评论:这有点伤害我的眼睛...这不是您编写 Angular 的方式;) 不要那样访问 dom...
-
您可以简单地将
[disabled]属性绑定与所需条件结合使用。 -
我需要用另一个选中的复选框绑定禁用,这对我在运动中的文本框起作用
标签: angular typescript radio-button