Reactive Form을 사용하며 custom input 컴포넌트를 사용하고 있었는데, disabled 속성을 사용하자 콘솔창에 다음과 같은 경고가 발생하였다.
It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
when you set up this control in your component class, the disabled attribute will actually be set in the DOM for
you. We recommend using this approach to avoid 'changed after checked' errors.
Example:
// Specify the `disabled` property at control creation time:
form = new FormGroup({
first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
last: new FormControl('Drew', Validators.required)
});
// Controls can also be enabled/disabled after creation:
form.get('first')?.enable();
form.get('last')?.disable();
<input formControlName="total" [disabled]="true"></input>
<input formControlName="total"></input>
form = new FormGroup({
...
total: new FormControl({ value: 0, disabled: true }, Validators.required),
});
form.get('first')?.enable();
form.get('last')?.disable();
ControlValueAccessor
를 통해 정의한 setDisabledState
함수를 통해 disabled
값을 가져온다.disabled = signal<boolean>(false);
setDisabledState?(isDisabled: boolean): void {
this.disabled.set(isDisabled);
}
<custom-input
...
[disabled]="disabled()"
></custom-input>
References