I'm trying to check model of form1 from form2. How can I get it working? Here's jsbin describing what I would like to have. http://jsbin.com/kuluhataka/edit?js,output
问问题
1331 次
1 回答
1
我强烈建议不要做你想做的事情。类型只能用于字段,不能用于表单。将 Angular 用于它擅长的领域,正式地用于它擅长的领域。
需要注意的另一件事是expressionProperties
仅在表单的给定模型更改时运行。这就是为什么您在更改其他字段的值时没有运行的原因(因为它们具有不同的模型属性)。
这是一个可行的解决方案: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>
于 2015-08-26T21:57:21.953 回答