1

i am new to angular, i am trying to use Jquery with angular 4, i found this question on stack over flow and in side the question i found this example

but when i try to impliment it i get the following error :

vendor.bundle.js:51867 ERROR Error: Uncaught (in promise): TypeError: $(...).chosen is not a function
TypeError: $(...).chosen is not a function
    at PocTestsPage.webpackJsonp.../../../../../src/app/structure/poc-tests/poc-tests.page.ts.PocTestsPage.ngAfterViewInit (main.bundle.js:898)
    at callProviderLifecycles (vendor.bundle.js:61944)
    at callElementProvidersLifecycles (vendor.bundle.js:61919)
    at callLifecycleHooksChildrenFirst (vendor.bundle.js:61903)
    at checkAndUpdateView (vendor.bundle.js:62935)
    at callWithDebugContext (vendor.bundle.js:63989)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (vendor.bundle.js:63529)
    at ViewRef_.webpackJsonp.../../../core/@angular/core.es5.js.ViewRef_.detectChanges (vendor.bundle.js:61001)
    at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (vendor.bundle.js:83416)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.placeComponentIntoOutlet (vendor.bundle.js:82597)
    at PocTestsPage.webpackJsonp.../../../../../src/app/structure/poc-tests/poc-tests.page.ts.PocTestsPage.ngAfterViewInit (main.bundle.js:898)
    at callProviderLifecycles (vendor.bundle.js:61944)
    at callElementProvidersLifecycles (vendor.bundle.js:61919)
    at callLifecycleHooksChildrenFirst (vendor.bundle.js:61903)
    at checkAndUpdateView (vendor.bundle.js:62935)
    at callWithDebugContext (vendor.bundle.js:63989)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (vendor.bundle.js:63529)
    at ViewRef_.webpackJsonp.../../../core/@angular/core.es5.js.ViewRef_.detectChanges (vendor.bundle.js:61001)
    at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (vendor.bundle.js:83416)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.placeComponentIntoOutlet (vendor.bundle.js:82597)
    at resolvePromise (polyfills.bundle.js:3179)
    at resolvePromise (polyfills.bundle.js:3150)
    at polyfills.bundle.js:3227
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.bundle.js:2833)
    at Object.onInvokeTask (vendor.bundle.js:54923)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.bundle.js:2832)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (polyfills.bundle.js:2600)
    at drainMicroTaskQueue (polyfills.bundle.js:2993)
    at ZoneTask.invoke (polyfills.bundle.js:2899)
    at timer (polyfills.bundle.js:3949)

i am using an angular 4 library called cleanUI:

i created a component called poc-tests as shown in the image : enter image description here

poc-tests.html:

<!DOCTYPE html>
<html>
<head>
  <title>Angular 2 QuickStart</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.min.css">

  <!-- 1. Load libraries -->
  <!-- Polyfill(s) for older browsers -->
  <script src="https://npmcdn.com/core-js/client/shim.min.js"></script>

  <script src="https://npmcdn.com/[email protected]?main=browser"></script>
  <script src="https://npmcdn.com/[email protected]"></script>
  <script src="https://npmcdn.com/[email protected]/dist/system.src.js"></script>

  <!-- jQuery and chosen -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.jquery.min.js"></script>


  <!-- 2. Configure SystemJS -->
  <script src="systemjs.config.js"></script>
  <script>
    System.import('app').catch(function(err){ console.error(err); });
  </script>
</head>

<!-- 3. Display the application -->
<body>
<ng-chosen>Loading...</ng-chosen>
</body>
</html>

poc-tests.page.ts:

import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

declare var $: any;
declare var jQuery: any;
declare var autosize: any;
declare var Ladda: any;
declare var Chartist: any;

@Component({
  selector: 'ng-chosen',
  template: `<select #selectElem>
    <option *ngFor="let item of items" [value]="item" [selected]="item === selectedValue">{{item}} option</option>
  </select>
  <h4> {{selectedValue}}</h4>`
})
export class PocTestsPage implements AfterViewInit {
  @ViewChild('selectElem') el:ElementRef;
  items = ['First', 'Second', 'Third'];
  selectedValue = 'Second';

  ngAfterViewInit() {
    $(this.el.nativeElement)
      .chosen()
      .on('change', (e, args) => {
        this.selectedValue = args.selected;
      });
  }
}

4
  • Is chosen installed? Are you sure the issue is with jQuery, and not that chosen isn't loaded? Commented Jun 17, 2017 at 21:19
  • what you mean by chosen ? Commented Jun 17, 2017 at 21:21
  • why are you using jQuery any specific reason? Commented Jun 17, 2017 at 21:24
  • Your error specifically states that ` $(...).chosen` is not a function. chosen is a jQuery library. It's being used somehow, the question is, are you loading it? Commented Jun 17, 2017 at 22:08

1 Answer 1

2

You need to install @types/jquery and remove all the declare var related to jQuery:

npm install @types/jquery --save

If you are also using webpack to build static files, you'll need this plugin:

new webpack.ProvidePlugin({
  $: "jquery",
  jQuery: "jquery"
}),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.