0

angular-formly(我使用的是 v6.4.xw/AngularJS 1.2.x)字段的表达式属性导致该字段在启用和禁用之间动态切换时,切换到禁用不会从模型中删除该字段的任何先前输入的值。例如:

  1. 使用Formly 的高级布局示例
  2. 输入名字,以便启用姓氏
  3. 输入姓氏的值
  4. 删除名字值,以便再次禁用姓氏
  5. 请注意,模型仍然包含为姓氏输入的数据,即使它被禁用

鉴于disabled标准 HTML 表单提交的工作原理,我希望模型不再包含该字段的值。

我需要避免为已禁用的字段提交数据,所以如果这是预期的行为(我不知道它是......),有人可以提供有关如何在任何时候完成删除值的expressionProperties想法要禁用的字段?

4

1 回答 1

1

kentcdodds' comments got me moving in the right direction and able to find enough pieces in the docs to make something work. In my most immediate task I was needing a radio option to get removed from the model when disabled, and I ended up unchecking it too. I did this by extending the radio input type and adding a link function for access to scope and the element. Below is something along the lines of what it looks like:

app.config(['formlyConfigProvider', function (formlyConfigProvider) {
    formlyConfigProvider.setType({
        name: 'liveDisableRadio',
        extends: 'radio',
        link: function (scope, element) {
            scope.$watch('to.disabled', function (is_disabled) {
                if (is_disabled) {
                    delete scope.model[scope.options.key];
                    element.find(':radio:checked').prop('checked', false).trigger('change');
                }
            });
        }
    });
}])
于 2015-06-17T03:30:14.073 回答