0

I am trying to use this https://github.com/ShinDarth/Nestable jquery plugin for nested drag and drop feature. I have already installed the jquery from npm install jquery --save and typings from npm install @types/jquery --save-dev

i have declared import * as $ from 'jquery'; in my component and included the plugin file in index.html page.

Here is my component code:

import { Component, OnInit,AfterViewInit,ElementRef } from '@angular/core';
import { MdDialog, MdDialogRef } from "@angular/material";
import { MediaUploadComponent } from '../helper-component/media-upload/media-upload.component';
import * as $ from 'jquery';
// import 'jquery';
// declare const $: JQueryStatic;
// declare var $:any;
// import $ from "jquery";

@Component({
  selector: 'app-appearance',
  templateUrl: './appearance.component.html',
  styleUrls: ['./appearance.component.css']
})
export class AppearanceComponent implements OnInit ,AfterViewInit{
  private selectedOption:string ;
  site_icon:any;
  constructor() { }

  ngOnInit() {
  }
  ngAfterViewInit(){
  // jquery plugin nestable
    $('.nestable').nestable();
  //console.log(jQuery.fn.jquery);
  }
}

After all it throws error : enter image description here

How can i fix this problem?

[i have already tried including the jquery link to index page from the cdn but shows the same error]

Index.html

!doctype html>
<html>
<head>
  <meta charset="utf-8">

  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="../assets/bootstrap.min.css">
  <!--<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> -->

  <script src="../assets/nestable/jquery.nestable.js"></script>
  <script src="../assets/nestable/jquery.nestable++.js"></script>


</head>

....

4
  • Can you show index.html Commented Jun 18, 2017 at 19:51
  • @Ced Yes i have updated the question Commented Jun 18, 2017 at 19:55
  • you are using angular cli ? Commented Jun 18, 2017 at 19:59
  • yes i am using angular cli @Ced Commented Jun 18, 2017 at 20:00

1 Answer 1

7

Ultimately you'd want to install jquery and other libraries in the angular-cli.json

scripts: [
"../node_modules/jquery/dist/jquery.min.js",
"../assets/nestable/jquery.nestable.js",
"../assets/nestable/jquery.nestable++.js"
]

The changes will only apply once you restart ng serve.

EDIT: For further reading and explanation this article might be of help https://github.com/angular/angular-cli/wiki/stories-global-scripts

Sign up to request clarification or add additional context in comments.

2 Comments

after this the error is same : TypeError: $(...).nestable is not a function(…)
@SurajKhanal you have to restart the cli (ctrl+c then ng serve again) you also have to remove the scripts you included in index.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.