简体   繁体   English

角度 8 中的 Jquery 日期选择器

[英]Jquery Date Picker in angular 8

I'm trying to implement datepicker using jQuery library because my requirement of datepicker is not matching with available intenet source.我正在尝试使用 jQuery 库实现 datepicker,因为我对 datepicker 的要求与可用的 Internet 源不匹配。 so I decide create my own datepicker using jquery UI所以我决定使用 jquery UI 创建我自己的日期选择器

I have included jQuery UI in index.html我在 index.html 中包含了 jQuery UI

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

and in the component并在组件中

  Start Date : <input type="text" #sd name="startDate" id="startDate"/>

in th ts file在 ts 文件中

declare var $: any;

$( '#startDate' ).datepicker({
      dateFormat: 'dd/mm/yy',
      firstDay: 1
    });

but every time I'm getting error但每次我收到错误

core.js:7187 ERROR Error: Uncaught (in promise): TypeError: $(...).datepicker is not a function TypeError: $(...).datepicker is not a function core.js:7187 错误错误:未捕获(承诺):TypeError:$(...).datepicker 不是函数 TypeError:$(...).datepicker 不是函数

I tried all the available answers in the Stack Overflow.我尝试了 Stack Overflow 中所有可用的答案。

jQuery doesn't have a built in datepicker, have you included the datepicker plugin? jQuery 没有内置日期选择器,您是否包含了日期选择器插件?

But we don't use jQuery with Angular, find an Angular datepicker.但是我们不将 jQuery 与 Angular 一起使用,而是找到一个 Angular 日期选择器。

In my opinion, instead of invest time in creating your own date picker with all the efforts that it will take you.在我看来,与其花时间创建您自己的日期选择器,不如花费您所有的努力。 I recommend you to use Angular Material .我建议您使用Angular Material It's done to work perfectly with Angular, developed and maintained by Google (as well as Angular).它与由 Google(以及 Angular)开发和维护的 Angular 完美配合。

They have already implemented a Datepicker that works perfectly: https://material.angular.io/components/datepicker/overview他们已经实现了一个完美运行的 Datepicker: https : //material.angular.io/components/datepicker/overview

Also, they have a lot of reusable components and features that are always necessary for all the projects.此外,它们具有许多可重用的组件和功能,这些组件和功能对于所有项目都是必需的。

After declaring in your index.html file在 index.html 文件中声明后

You need to make sure , that you call $( '#startDate' ).datepicker after your view is rendered on the UI, To do this you can add your code inside the method ngAfterContentChecked or ngAfterViewChecked, Once you add your code inside these methods of the Component, your date picker will start working.您需要确保在 UI 上呈现视图后调用 $( '#startDate' ).datepicker,为此,您可以在 ngAfterContentChecked 或 ngAfterViewChecked 方法中添加代码,在这些方法中添加代码后组件,您的日期选择器将开始工作。

Also instead of directly putting your code inside the component just like也不要像这样直接将代码放在组件中

$( '#startDate' ).datepicker({
      dateFormat: 'dd/mm/yy',
      firstDay: 1
});

Create a function inside a javascript file(example.js) in your angular application like below在您的 Angular 应用程序中的 javascript 文件(example.js)中创建一个函数,如下所示

function loadDatePicker(){
  $( '#startDate' ).datepicker({
      dateFormat: 'dd/mm/yy',
      firstDay: 1
   });
}

Declare your function inside the top of your component class where you want to use it like below在组件类的顶部声明你的函数,你想像下面这样使用它

declare function loadDatePicker(): any声明函数 loadDatePicker(): 任何

and then create ngAfterContentChecked where you call like below然后创建 ngAfterContentChecked 在你调用的地方,如下所示

ngAfterContentChecked (){
  loadDatePicker();
}

This works like a charm for me.这对我来说就像一种魅力。

But instead of this, I suggest you build a datepicker directive(which can manupulate the DOM) and specify the directive on your input element where you want date picker.但不是这样,我建议您构建一个 datepicker 指令(它可以操纵 DOM)并在您想要日期选择器的输入元素上指定该指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM