Angular-Formly: Hide/Disable Fields from Service

user2298830 picture user2298830 · Oct 2, 2015 · Viewed 9k times · Source

I have a service in which i try to disable an angular-formly field:

formField.templateOptions.disabled = true;

Even though the templateOption object is correct, the field is not disabled:

"templateOptions": {
    "type": "text",
    "placeholder": "",
    "label": "Single Line Test",
    "disabled": true
  },

Is this not possible?

Thanks.

Edit: Form Code before disabling in service

{
  "type": "input",
  "key": "single_line_test",
  "templateOptions": {
    "type": "text",
    "placeholder": "",
    "label": "Single Line Test"
  },
  "data": {},
  "validation": {
    "messages": {},
    "errorExistsAndShouldBeVisible": false
  },
  "id": "formly_2_input_single_line_test_3",
  "name": "formly_2_input_single_line_test_3",
  "formControl": {
    "$validators": {},
    "$asyncValidators": {},
    "$parsers": [],
    "$formatters": [
      null
    ],
    "$viewChangeListeners": [],
    "$untouched": true,
    "$touched": false,
    "$pristine": true,
    "$dirty": false,
    "$valid": true,
    "$invalid": false,
    "$error": {},
    "$name": "formly_2_input_single_line_test_3",
    "$options": null
  }
},

Answer

kentcdodds picture kentcdodds · Oct 2, 2015

If you want a property to be dynamic, you must use expressionProperties. Technically, you don't have to see it via an expression property, but it needs to be present in expression properties because formly has an optimization that basically says if it's not on there, then it won't watch for changes. This is a performance optimization.

You can see dynamic disabled working in this example: http://angular-formly.com/#/example/intro/codementor