我尝试在我的表单字段上显示描述,但它没有出现。
我试图在架构和运行时包含描述,但在这两种情况下,它都不会出现。
Schema.json :
{
"key": "CommentText",
"type": "input",
"className": "form-group",
"templateOptions": {
"label": "Comment text",
"required": true,
"maxLength": 1000,
"description": "test"
}
}
目的是显示具有 maxlength 属性的字段的字符倒计时。但首先我只需要显示一个基本描述。
最小/最大长度应该使用验证器来完成。
validators: [
],
validationMessages: [
{ name: MaxLengthValidator.validatorName, message: MaxLengthValidator.message },
],
但是在我的代码中,我创建了带有模板描述的自定义输入:
<input class="formly-string-input"
type="text"
[formControl]="formControl"
[formlyAttributes]="field"/>
<span *ngIf="field.templateOptions.description" class="form-control-info">{{field.templateOptions.description}}</span>
而且我认为 ngx 形式的输入没有要显示的描述
输入代码
import { Component } from '@angular/core';
import { FieldType } from '@ngx-formly/core';
@Component({
selector: 'formly-field-input',
template: `
<input *ngIf="type !== 'number' else numberTmp" [type]="type" [formControl]="formControl" class="form-control" [formlyAttributes]="field" [class.is-invalid]="showError">
<ng-template #numberTmp>
<input type="number" [formControl]="formControl" class="form-control" [formlyAttributes]="field" [class.is-invalid]="showError">
</ng-template>
`,
})
export class FormlyFieldInput extends FieldType {
get type() {
return this.to.type || 'text';
}
}