Search code examples
angular-formly

Angular-Formly: Reset Field Model When Hidden


Using Angular Formly, I am using 'hideExpression' to hide a field based on another field's model value. This works as expected. However, I need to reset the hidden fields model value when it becomes hidden. How can this be accomplished in Angular-Formly? Is there some type of event I can hook?


Solution

  • you can use hideExpression callback and set model value before return true or false

    {
                key: 'model_key',
                type: 'input',
                templateOptions: {
                    label: 'Your label'
                },
                hideExpression: function ($viewValue, $modelValue, scope) {
                    var hide = true; // replace true with your condition
                    if (hide) {
                         vm.model.model_key = '';
                    }
                    return hide;
                }
            }