Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery is not defined for angular 2

I am trying to use ng2-datetime. However, when I follow the instruction and installed npm install --save ng2-datetime, I get the following error.

jQuery not defined

I have tried running the following npm install but it doesnt fix anything.

 npm install jquery --save 
 npm install @types/jquery --save-dev

My package.json file

  {
  "name": "timesheet-web-app",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.3.1",
    "@angular/compiler": "^2.3.1",
    "@angular/core": "^2.3.1",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.3.1",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "^3.3.1",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.18",
    "angularfire2": "^2.0.0-beta.7",
    "bootstrap": "^4.0.0-alpha.6",
    "bootstrap-datepicker": "^1.6.4",
    "core-js": "^2.4.1",
    "firebase": "^3.6.6",
    "jquery": "^3.1.1",
    "ng2-datetime": "^1.2.2",
    "ng2-datetime-picker": "^0.12.7",
    "ng2-file-upload": "^1.2.0",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^2.3.1",
    "@types/jasmine": "2.5.38",
    "@types/jquery": "^2.0.39",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.24",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.0.2",
    "typescript": "~2.0.3"
  }
}

My folder structure

like image 359
user172902 Avatar asked Jan 23 '17 08:01

user172902


People also ask

Why jQuery is not used in Angular?

It adds a lot to bundle size which is very bad for slow networks and CPUs (mobile!). Selectors and events are usually solved by libraries like React and Angular, so you don't need jQuery to help with browser compability and API differences.

How do you fix ReferenceError is not defined?

Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

Is not defined in Angular?

While programming in JavaScript, jQuery or Angular JS, coders often encounter an error called Uncaught ReferenceError: $ is not defined. This usually happens when the $, that may be a variable or a method is not properly defined but is being used.


1 Answers

You need to import jquery with angular-cli. Edit your angular-cli.json file.

Find script array and add jquery.

"scripts": [
     "../node_modules/jquery/dist/jquery.min.js"
],
like image 99
Kacper Polak Avatar answered Oct 13 '22 00:10

Kacper Polak