Sireini
Sireini

Reputation: 4262

Why is $ not recognised in angular 2?

I have included this in my typings.json file:

    {
  "ambientDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654",
    "systemjs": "registry:dt/systemjs#0.18.4+20160417155658",
    "jquery": "github:DefinitelyTyped/DefinitelyTyped/jquery/jquery.d.ts#661fc869669af796c6048483e999dce5732eed72"
  }
}

and the structure looks like this :

enter image description here

But I'll get the error that $ is not defined

Upvotes: 3

Views: 72

Answers (1)

David Pine
David Pine

Reputation: 24535

In order to use global variables such as jQuery in an Angular2 TypeScript model you can do the following:

import // omitted for brevity...

declare var jQuery; // Declare it outside the scope of your class.

export class FooBar {
   constructor() {
       jQuery('selector').blah(...); // Use it
}

I have used this a few times, but do so sparingly. It is certainly frowned upon... I have only used the fully qualified name jQuery, but I assume you could use $ as well.

Upvotes: 3

Related Questions