【问题标题】:Datepicker ngx-bootstrap日期选择器 ngx-bootstrap
【发布时间】:2018-01-17 13:26:53
【问题描述】:

我正在尝试在旧日期选择器上应用语言环境

https://valor-software.com/ngx-bootstrap/#/datepicker

我已经尝试了 evrything,但我不知道该往哪里看,我可以使用新的,但我有很大的解决方案,我必须留在旧的日期选择器上

这就是我所拥有的

my.module.ts

import { CommonModule } from '@angular/common';
import { DatepickerModule } from 'ngx-bootstrap/datepicker';

@NgModule({
    imports: [
        DatepickerModule.forRoot() 
    ],
    declarations: [

    ],
    providers: [

    ],
    schemas: [
        CUSTOM_ELEMENTS_SCHEMA
    ]
})
export class MyModule {
}

my.component.ts

import { defineLocale } from 'ngx-bootstrap/bs-moment';
import { de } from 'ngx-bootstrap/locale';
defineLocale('de', de);


@Component({
    templateUrl: './my.component.html',
    styleUrls: ['./my.component.scss']
})

export class MyComponent implements OnInit {}

my.component.html

  <datepicker formControlName="effectiveDate" [showWeeks]="false" [locale]="de">

无法绑定到“locale”,因为它不是“datepicker”的已知属性。 :(

【问题讨论】:

  • 对于这个日期选择器的旧版本,我没有看到 locale 列为可用输入。
  • 是的,但是如何为旧版本添加语言环境,这是我的问题?
  • 您不能只是简单地将输入添加到第三方库而不分叉和修改源代码。为什么不使用更新版本?它包含一个locale
  • 我无法更新
  • 看起来旧版本和新版本在完全相同的库中,只需更改您导入的模块即可。一个是DatepickerModule,新的一个是BsDatepickerModule

标签: angular datepicker ngx-bootstrap


【解决方案1】:

此日期选择器的旧版本不包含 locale 输入。文档状态 This is a legacy version of datepicker without support of daterangepicker, locales, themes, etc. 较新的版本确实有一个语言环境,但它不能作为输入属性进行修改。相反,您使用 BsLocaleService 更改语言环境。它应该看起来像这样:

my.module.ts

import { CommonModule } from '@angular/common';
import { BsDatepickerModule, BsLocaleService  } from 'ngx-bootstrap/datepicker';
import { de } from 'ngx-bootstrap/locale';
defineLocale('de', de);

@NgModule({
    imports: [
        BsDatepickerModule.forRoot() 
    ],
    declarations: [

    ],
    providers: [
        BsLocaleService 
    ],
    schemas: [
        CUSTOM_ELEMENTS_SCHEMA
    ]
})
export class MyModule {
}

my.component.ts

import { BsDatepickerConfig, BsLocaleService } from 'ngx-bootstrap/datepicker';
import { listLocales } from 'ngx-bootstrap/bs-moment';

@Component({
    templateUrl: './my.component.html',
    styleUrls: ['./my.component.scss']
})

export class MyComponent implements OnInit {
   locale = 'de';

   constructor(private _localeService: BsLocaleService) {}

   ngOnInit(){
      this._localeService.use(this.locale);
   }

}

my.component.html

<input placeholder="Datepicker" type="text" formControlName="effectiveDate" class="form-control" bsDatepicker #dp="bsDatepicker">

【讨论】:

  • 未捕获(承诺):错误:没有 BsLocaleService 的提供者!
  • 抱歉,在providers 中没有提及。已更新。
  • 我在你写的时候改成了 BsDatePicker,但是现在我得到了这个错误 Error: Template parse errors: 'datepicker' is not a known element: 1. 如果 'datepicker' 是一个 Angular 组件,那么验证它是这个模块的一部分。 2. 要允许任何元素添加“NO_ERRORS_SCHEMA”到该组件的“@NgModule.schemas”。 ("
    [ERROR ->]
  • 我的错。我正在尝试更新您的代码,并没有说明新版本是指令而不是组件,因此请使用 bsDatepicker 指令。
  • 这没有回答我的问题,我们已经更新了 datepicker,我不需要更新旧 datepicker 上的语言环境
  • 猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多