如何解决在ngx-formly中使用hideExpression的语法错误
我创建了一个组件,以允许用户构建调查表。目标是将提供的所有信息存储到FormlyFieldConfig中,然后将其另存为JSON到数据库中。满足期望条件时将生成问卷。然后,我将JSON解析为FormlyFieldConfig,以显示为表单。
我遇到的问题与“ hideExpression”有关。如果问题的类型为“选择”,则其选项中可能还有其他后续问题。表单会很好地保存,以防出现以下错误;
期望的表达式,得到'!=='
这是我创建hideExpression的方法:
hideExpression: 'model.' + keyvalue + ' !== ' + '"' + this.answerOptions[i].option + '"'
private getEditedQuestion(): FormlyFieldConfig {
const formModel = this.questionProfileForm.value;
var fieldConfig: FormlyFieldConfig = {};
// Here,we set the key using the questionLabel value without spaces
var keyvalue = formModel.questionLabel.replace(/\s/g,"")
// We start our field config with a 'CardWrapper'. Then,nest the fields in a field group
fieldConfig.wrappers = ['CardWrapper'];
fieldConfig.fieldGroupClassName = 'display-flex-column';
fieldConfig.fieldGroup = [
{
key: keyvalue,type: formModel.answerType.type,templateOptions: {
label: formModel.questionLabel
}
}
]
switch (formModel.answerType.type) {
case 'input':
fieldConfig.fieldGroup[0].templateOptions.type = formModel.answerType.secondaryType;
switch (formModel.answerType.secondaryType) {
case 'text':
fieldConfig.fieldGroup[0].templateOptions.minLength = formModel.answerMinLength || 2;
fieldConfig.fieldGroup[0].templateOptions.maxLength = formModel.answerMaxLength || 100;
break;
case 'number':
fieldConfig.fieldGroup[0].templateOptions.min = formModel.answerMinNumber || 1;
fieldConfig.fieldGroup[0].templateOptions.max = formModel.answerMaxnumber || 1000000;
break;
default:
break;
}
break;
case 'textarea':
fieldConfig.fieldGroup[0].templateOptions.maxLength = formModel.answerTextAreaMaxLength || 2000;
fieldConfig.fieldGroup[0].templateOptions.cols = formModel.answerTextAreaColumns || 100;
fieldConfig.fieldGroup[0].templateOptions.rows = formModel.answerTextAreaRows || 3;
break;
case 'select':
// If the answer type is set to 'select' and answerOptions have actual values,we need to add them to the field group of the current fieldConfig
if (this.answerOptions.length > 0) {
var optionsArray: any[] = [];
// Loop through the answerOptions and push them to an array
for (var i = 0; i < this.answerOptions.length; i++) {
optionsArray.push({ label: this.answerOptions[i].option,value: this.answerOptions[i].option });
// If the answerOption has a subquestion(s),we need to build additonal field configs to be displayed when the answerOption value is selected
if (this.answerOptions[i].hasSubQuestion && this.answerOptions[i].subQuestions?.length > 0) {
var fieldArray: FormlyFieldConfig[] = [];
// Loop through the subQuestions,create fieldConfig objects for each,and push to a fieldConfig array
for (var j = 0; j < this.answerOptions[i].subQuestions.length; j++) {
var subkeyvalue = this.answerOptions[i].subQuestions[j].question.replace(/\s/g,"");
var subFieldConfig: FormlyFieldConfig = {
key: subkeyvalue,type: this.answerOptions[i].subQuestions[j].inputType.type,templateOptions: {
type: this.answerOptions[i].subQuestions[j].inputType.secondaryType ? this.answerOptions[i].subQuestions[j].inputType.secondaryType : '',label: this.answerOptions[i].subQuestions[j].question,description: this.answerOptions[i].option
},hideExpression: 'model.' + keyvalue + ' !== ' + '"' + this.answerOptions[i].option + '"'
};
if (this.answerOptions[i].subQuestions.length > 1) subFieldConfig.className = 'child-flex-grow';
fieldArray.push(subFieldConfig);
}
var fieldConfigRow: FormlyFieldConfig = {};
fieldConfigRow.fieldGroupClassName = 'display-flex-row';
fieldConfigRow.fieldGroup = fieldArray;
fieldConfig.fieldGroup.push(fieldConfigRow);
}
}
fieldConfig.fieldGroup[0].templateOptions.options = optionsArray;
}
default:
break;
}
return fieldConfig;
}
错误从formly library中的以下位置发生;第121行;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。