Good day!

I try to make a reactive form in which one of the inputs can have a readonly property depending on the state of the checkbox - if the checkbox is selected, the input is available for entering text, if not selected, input readonly.

Component:

import {FormControl, FormGroup, Validators} from "@angular/forms"; @Component({ selector: 'app-add-topic', templateUrl: './add-topic.component.html', styleUrls: ['./add-topic.component.css'] }) export class AddTopicComponent implements OnInit { constructor() { } addTopicForm: FormGroup; ngOnInit() { this.initForm(); } initForm(): void { this.addTopicForm = new FormGroup({ 'tabName': new FormControl(null, Validators.required), 'indicatorName': new FormControl(null, Validators.required) }); } } 

Template:

 <form class="addTopic modal" [formGroup]="addTopicForm"> <div class="project-label">Добавление топика</div> <div class="controls form-group"> <label for="tabName">Название</label> <input type="text" id="tabName" formControlName="tabName"> <label for="indicatorName">Индикатор</label> <span><input type="checkbox" id="isIndicator" #isInd checked></span> <input type="text" id="indicatorName" [readonly]="!isInd.checked" formControlName="indicatorName"> </div> </form> 

Actually, it seems to work this way, but the readonly property is set from the noodle through time (regardless of the state of the checkbox, it is necessary to change the state of the checkbox several times and even push it itself so that it has the desired property). What am I doing wrong?

  • it is not "put on the bald", this property is not tracked at all, so the isInd.checked construction isInd.checked useless, use ngModel - overthesanity
  • @overthesanity is generally surprising, since Input is still put Ridonly. then I do not know why it is put there. So, isn’t the use of ngModel in reactive forms not deprecated? .. - fiorsaoirse pm
  • type="checkbox" id="isIndicator" #isInd you have without formControlName - overthesanity
  • hmm, logical, thank you! I will go to explore further - fiorsaoirse 5:58 pm

0