ng-admin的引用字段中的条件默认值

时间:2018-06-18 10:06:51

标签: javascript angularjs reference default-value ng-admin

根据ng-admin的文档,可以为参考字段定义默认值。

实施例。

nga.field('country_id', 'reference')
.targetEntity(admin.getEntity('countries'))
.targetField(nga.field('country_name'))
.defaultValue(1)
.label('Choose a country from the list')

如果默认国家/地区取决于条件怎么办?例如,我想将选项'Other'设置为默认值。但是,列表中可能不存在“其他”选项,因为用户可能希望将其作为选项删除。在这种情况下,我想将列表中的第一个国家/地区设置为默认国家/地区。

理论上我会做这样的事情:

nga.field('country_id', 'reference')
.targetEntity(admin.getEntity('countries'))
.targetField(nga.field('country_name'))
.defaultValue(function(entry){
    if(/*check if id= 1 exists*/){
        return {value: 1, label: 'Other'};
    }
    else{
        return {value: 2, label: 'Afghanistan'};
    }
})
.label('Choose a country from the list')

无论我在内部使用什么条件,或者即使我完全删除条件并只返回一个值,只要我在defaultValue中包含一个函数,就会生成此错误:

  

无法读取未定义的属性“值”

是否可以在defaultValue属性中注入一个函数?

0 个答案:

没有答案