2

I want to use the jQuery UI datepicker with Angular 5, but it doesn't work...

First I wrote a simple html file without Angular:

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>datepicker demo</title>
  <link rel="stylesheet" href="../node_modules/jquery-ui/themes/base/all.css">
  <script src="../node_modules/jquery/dist/jquery.min.js"></script>
  <script src="../node_modules/jquery-ui/ui/widgets/datepicker.js"></script>
</head>

<body>
<div id="datepicker"></div>
<script>
$( "#datepicker" ).datepicker();
</script>
</body>
</html>

This works great! When I open the file in a browser, the datepicker is inserted in the div. Now i want to use the datepicker inside of an angular component. Here are my files:

define-filter-component.component.ts:

import { Component, OnInit } from '@angular/core';

import * as $ from 'jquery';
import 'jquery-ui';

@Component({
  selector: 'app-define-filter-component',
  templateUrl: './define-filter-component.component.html',
  styleUrls: ['./define-filter-component.component.css']
})
export class DefineFilterComponentComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    $('#datepickerdiv').html('Hi there!');    // This is working!
    $('#datepickerdiv').datepicker();     // Browser says "TypeError: $(...).datepicker is not a function"
  }
}

define-filter-component.component.html:

<p>
  define-filter-component works!
</p>
<div id="datepickerdiv">XXX</div>

The scripts and styles in angular-cli.json:

      "styles": [
        "../node_modules/semantic-ui-css/semantic.css",
        "../node_modules/jquery-ui/themes/base/all.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/jquery-ui/ui/widgets/datepicker.js"
      ],

typings.d.ts (I added the reference tags):

/// <reference path="../node_modules/@types/jquery/index.d.ts" />
/// <reference path="../node_modules/@types/jqueryui/index.d.ts" />

/* SystemJS module definition */
declare var module: NodeModule;
interface NodeModule {
  id: string;
}

package.json:

{
  "name": "trace-detective",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "@types/jqueryui": "^1.12.1",
    "core-js": "^2.4.1",
    "jquery": "^3.3.1",
    "jquery-ui": "^1.12.1",
    "jquery-ui-themes": "^1.12.0",
    "rxjs": "^5.5.6",
    "semantic-ui-css": "^2.2.12",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "1.6.5",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/jquery": "^3.3.0",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }
}

When running ng serve, I got this error message:

ERROR in ./node_modules/css-loader?{"sourceMap":false,"import":false}!./node_mod
ules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./node_modules/jqu
ery-ui/themes/base/all.css
Module build failed: Error: Can't resolve 'base.css' in 'C:\Users\lotzevol\Deskt
op\TraceDetective\node_modules\jquery-ui\themes\base'
at onError (C:\Users\lotzevol\Desktop\TraceDetective\node_modules\enhanced-r
esolve\lib\Resolver.js:61:15)

Why "Can't resolve base.css"? The file does exist. Really!

I just removed the file .../all.css from angular-cli.json. The datepicker should also work without the CSS, even if it is not looking good.

Now the Compiler says: 'webpack: Compiled successfully'. Great!

I opened the Browser, but the datepicker doesn't appear! The Browser console says: TypeError: $(...).datepicker is not a function. What did I do wrong? It seems Angular doesn't like me...

I think there is something wrong with my import statements in define-filter-component.component.ts. Can anyone help please?

6

0