简体   繁体   English

基于Angular 6反应形式的单选按钮禁用textarea

[英]Disable textarea on the basis of radio button in Angular 6 reactive form

I have a reactive form that is initialized on page refresh and get disabled/enabled on the basis of the state on the radio button which is bound with ngModel, the problem is the reactive form has another set of radio buttons, star rating and textarea.我有一个反应式表单,它在页面刷新时初始化,并根据与 ngModel 绑定的单选按钮上的状态禁用/启用,问题是反应式表单有另一组单选按钮、星级和 textarea。 Everything gets disabled as per demand except the textarea除 textarea 外,所有内容都根据需要被禁用

   <div class="radio-selection">
         <p-radioButton name="yes" value="yes" label="Yes" [(ngModel)]="attended" inputId="yes" (ngModelChange)="resetTextarea()">
         </p-radioButton>
         <p-radioButton name="no" value="no" label="No" [(ngModel)]="attended" inputId="no" (ngModelChange)="resetForm()">
         </p-radioButton>
    </div>

<div class="feedback-wrapper">

        <textarea pInputTextarea [(ngModel)]="not_attended_reason" [disabled]="attended==='yes'" placeholder="The reason for not attending the traning">
        </textarea>
        <form [formGroup]="trainingFeedbackForm" *ngIf="feedbackQuestions">
             <div formArrayName="feedback">
                 <ul class="questions">
                     <li class="list" *ngFor="let question of feedbackQuestions; index as i" [formGroupName]="i">
                           <div class="ques-title">{{question.text}}</div>
                           <div class="ques-desc">{{question.description}}</div>
                           <div *ngIf="question.type === feedbackQuestionTypeRef.Text; else mcq">
                           <textarea pInputTextarea formControlName="answer" [disabled]="attended==='no'"></textarea>
                            </div>
                            <ng-template #mcq>
                                 <p-radioButton *ngFor="let mcq of question.options" name={{question.id}} [disabled]="attended==='no'" label={{mcq.choice}} formControlName="answer" [value]="mcq.choice"></p-radioButton>
                            </ng-template>
                      </li>
                  </ul>
             </div>
             <div class="training-rating">
                 <div class="ques-title">Overall Rating</div>
                     <p-rating [cancel]="false" stars="5" formControlName="rating" [disabled]="attended==='no'"></p-rating>
             </div>
         </form>
    </div>

Now, the radio buttons which enable or disable the form are:现在,启用或禁用表单的单选按钮是:

<p-radioButton name="yes" value="yes" label="Yes" [(ngModel)]="attended" inputId="yes" (ngModelChange)="resetTextarea()">
</p-radioButton>
<p-radioButton name="no" value="no" label="No" [(ngModel)]="attended" inputId="no" (ngModelChange)="resetForm()"></p-radioButton>

it disables the form as needed but not the textarea can someone help me in this它根据需要禁用表单,但不是 textarea 可以帮助我

this.trainingFeedbackForm.controls['feedback'].disable();
this.trainingFeedbackForm.controls['rating'].disable();

this resolves这解决了

A simple way to do disable :一种简单的禁用方法:

<textarea disabled> 
     This textarea field is disabled. 
</textarea> 

For condition-based disable you can visit this blog对于基于条件的禁用,您可以访问此博客
which is done using directive这是使用指令完成的

Directive指示

import { NgControl } from '@angular/forms';

@Directive({
  selector: '[disableControl]'
})
export class DisableControlDirective {

  @Input() set disableControl( condition : boolean ) {
    const action = condition ? 'disable' : 'enable';
    this.ngControl.control[action]();
  }

  constructor( private ngControl : NgControl ) {
  }

}

HTML HTML

<input class="form-control" placeholder="Name" name="name" formControlName="name" autocomplete="off" [disableControl]="isDisabled" required>

Here is another answer maybe it helps you这是另一个答案,也许对你有帮助

https://stackoverflow.com/a/50220894/11004482 https://stackoverflow.com/a/50220894/11004482

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

相关问题 Angular反应形式的单选按钮值为null - Radio button value in Angular reactive form is null 根据 Angular 中的 function 值禁用反应形式的单选按钮 - Disable radio buttons in reactive form based on function value in Angular 在 Angular Reactive Form 中禁用基于多个空 formControlNames 的按钮 - Disable a button based on multiple empty formControlNames in an Angular Reactive Form 如何在 Angular 中以反应形式禁用按钮? - How do I disable a button in a reactive form in Angular? 如果反应表单字段无效,则禁用兄弟按钮Angular 5 - Disable a sibling button if reactive form field is not valid Angular 5 反应式表单检查表单字段为空或默认并禁用按钮 Angular - Reactive Forms check form fields empty or default and disable button Angular 无法检查(单击)另一个单选按钮上的单选按钮:Angular Reactive Form - Unable to Check Radio-button on (click) of another Radio Button: Angular Reactive Form 禁用角度反应形式的字段 - Disable a field in angular reactive form 如何以反应形式传递单选按钮的值? - How to pass value for radio button in reactive form? 角度2/4 —将单选按钮[value]绑定到对象或函数时,为什么此反应式表单值不更新? - Angular 2/4 — When binding a radio button [value] to an object or function, why does this reactive form value not update?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM