0

How can I disable the checkbox without displaying this warning message?

"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."

This is my html code:

<ng-container *ngFor='let option of to.options' >
      <p-checkbox class='p-checkbox-label'
        [formControl]='formControl'
        [inputId]='option.key'
        [name]='to.key'
        [label]='option.label'
        [value]='option.key'
        [disabled]='option.disabled'
      ></p-checkbox>
</ng-container>

This is the code to create two checkboxes, where the second checkbox field is disabled

form: new FormGroup({}),
fields: [
      {`enter code here`
        key: 'value',
        type: 'checkbox-group',
        templateOptions: {
          options: [
            {
              key: 'selected',
              label: 'Selected',
              disabled: false
            },
            {
              key: 'selectedDisabled',
              label: 'Selected - Disabled',
              disabled: true
            }
          ]
        },
        hooks: {
          onInit: (field: any) => {
            field.form.setValue({
              'value': ['selected', 'selectedDisabled']
            });
          }
        }
      }
   ]
2
  • I havent used angular-formly but why do you create checkboxes in the template, shouldnt they be created in the component? I Also tried to create with type 'checkbox-group' but doesnt seem to find that type, only other type i could find was multicheckbox and it seems you cant disable a individual checkbox with that (?).
    – Tommi
    Mar 9, 2022 at 14:42
  • They are created in the component. I am creating a custom component using primeNg and I gave the name 'checkbox-group'
    – Sofi
    Mar 15, 2022 at 15:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.