0

我正在使用 ngx-formly/bootstrap(不使用材料)创建动态 UI/表单。我想显示 datepicker 控件,所以我使用 ngx-bootstrap/datepicker 显示了 custon bsdatepicker 控件。但是当我在 templateOptions 中给它时,标签没有显示

我尝试了以下方式 1. 创建一个包含用于日期选择器的 html 的组件

       import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
       //datepicker.component.html
       <input type="text" 
       id="dob-id" 
       class="form-control calendar" 
       placement="bottom" 
       bsDatepicker
       [formlyAttributes]="field"
       #dobDate="bsDatepicker" 
       [bsConfig]="bsConfig" 
       placeholder="YYYY-MM-DD"
       [class.is-invalid]="showError" class=""  style="width: 350px;">

       //datepicker.component.ts
       import { Component, OnInit } from '@angular/core';
       import { FieldType } from '@ngx-formly/core';
       import { BsDatepickerConfig } from 'ngx-bootstrap';

       @Component({
       selector: 'app-datepicker',
       templateUrl: './datepicker.component.html',
      styleUrls: ['./datepicker.component.scss']
      })

      export class CustomDatepickerComponent extends FieldType {
     // Optional: only if you want to rely on `MatInput` implementation
     bsConfig: Partial<BsDatepickerConfig> = {
     : 'YYYY-MM-DD',
     showWeekNumbers: false,
     containerClass: 'theme-dark-blue'    
     };
     }

在 app.module 我已经注册了该组件,并在我定义了架构的组件中,

    {
    key: 'date1',
    type: 'bsdatepicker',
    templateOptions: {
      label :'From Date',
      required: true
    },
    expressionProperties: {
      //'templateOptions.label': 'From Date'
    }
  },

任何帮助将不胜感激

4

1 回答 1

1

在为已经存在的 UI(例如引导程序)注册自定义字段类型时,您只需要使用定义的form-field包装器来呈现labelvalidation错误:

@NgModule({
  imports: [
    FormlyModule.forRoot({
      types: [
        {
          name: 'bsdatepicker',
          ...
          wrappers: ['form-field']
        },
      ],
    }),
  ],
})
export class AppModule { }
于 2020-03-12T13:12:02.357 回答