Angular-Formly:如果从下拉框中选择了选项,则需要填写一个字段

时间:2018-05-22 09:30:21

标签: javascript angularjs angular-formly

如果从相应的下拉框中选择了一个选项,我想要输入字段。一旦从下拉框中选择了一个选项,我的2个字段就会重复,因此用户不限于单个选项。所以...如果选择exportSupplier,则agreementReference变为required。但是,如果有多个exportSupplier且其中至少有一个未被选中,则表单会将agreementReference字段视为not required

如果选择了相应的agreementReference,如何制作相应的required字段exportSupplier

    vm.exportSuppliers = [{ exportSupplier: '', agreementReference: '' }];

    vm.exportSupplierFields = [
        {
            fieldGroup: [
                {
                    className: 'col-xs-6',
                    key: 'exportSupplier',
                    type: 'select2',
                    templateOptions: {
                        label: 'Export Supplier',
                        required: false,
                        options: [],
                        onChange: function() {
                            vm.addExportSupplier();
                        }
                    }
                },
                {
                    className: 'col-xs-5',
                    key: 'agreementReference',
                    type: 'input',
                    templateOptions: {
                        label: 'Agreement Reference',
                        onChange: function() {
                            vm.addExportSupplier();
                        }
                    },
                    expressionProperties: {
                        'templateOptions.required': '!!model["exportSupplier"]'
                    }
                }
            ]
        }
    ];

    vm.addExportSupplier = function() {
        if (vm.exportSuppliers[vm.exportSuppliers.length - 1].exportSupplier || vm.exportSuppliers[vm.exportSuppliers.length - 1].agreementReference) {
            vm.exportSuppliers.push({ exportSupplier: '', agreementReference: '' });
        }
    };

我的HTML

<div ng-repeat="supplier in vm.exportSuppliers" class="row">
    <formly-form model="supplier" fields="vm.exportSupplierFields"
        form="vm.informationExportSupplier.form" index="$index">
    </formly-form>
</div>

我尝试了不同的变体来将$ index添加到'templateOptions.required'字段中,但没有成功。

1 个答案:

答案 0 :(得分:0)

  

我尝试了不同的变体来将$ index添加到'templateOptions.required'字段中,但没有成功。

<div ng-repeat="supplier in vm.exportSuppliers" ng-init="outsideIndex = $index">
    <formly-form model="supplier" fields="vm.exportSupplierFields"
        form="vm.informationExportSupplier.form"  ̶i̶n̶d̶e̶x̶"̶$̶i̶n̶d̶e̶x̶"̶
        options="{data: {outsideIndex: outsideIndex}}">
    </formly-form>
</div>

来自文档:

  

options

     

表格的选项。

     

data

     

这是一个对象。把你想要的任何东西放在这里。

     

— Angular-formly formly-form Directive API Refeerence

相关问题