在两个 angular-formly 表单之间传递数据

Passing data beetween two angular-formly forms

我正在尝试从 form2 检查 form1 的模型。我怎样才能让它工作? 这是 jsbin 描述我想要的东西。 http://jsbin.com/kuluhataka/edit?js,output

我强烈建议不要做你想做的事。类型应该只用于字段,而不是用于表单。擅长的用angular,擅长的用formly。

另一件需要注意的重要事情是 expressionProperties 只有 运行 当表单的给定模型发生变化时。这就是为什么当您更改其他字段的值时您的 运行ning 不是(因为它们具有不同的模型属性)。

这是一个可行的解决方案:http://jsbin.com/resafa/1/edit?html,js,output

JavaScript

vm.model = {
  form1: {},
  form2: {}
};
vm.fields1 = [
  {
    key:'field1',
    model: 'model.form1',
    type: 'input',
    templateOptions: {
      label: 'form1.Input',
      placeholder: 'Formly is terrific!'
    }
  },
  {
    key:'field1',
    model: 'model.form2',
    type: 'input',
    templateOptions: {
      label: 'form2.Input',
      placeholder: 'Formly is terrific!'
    },
    expressionProperties: {
      'templateOptions.disabled': '!options.data.originalModel.form1.field1',
    },
    // https://github.com/formly-js/angular-formly/pull/443
    // when that ^ PR gets merged and released, then you don't have to do this step
    // and you can reference it as `options.originalModel` rather than `options.data.originalModel`
    // This should happen in the next day or so
    data: {
      originalModel: vm.model
    }
  }
];

HTML

<form ng-submit="vm.onSubmit()" name="vm.form" novalidate>
  <formly-form model="vm.model" fields="vm.fields1" options="vm.options" form="vm.form">            
  </formly-form>
  <formly-form model="vm.model" fields="vm.fields2" options="vm.options" form="vm.form">
  </formly-form>
</form>