0

I have a tree structure which uses jQuery. I had to use the same jQuery in my Angular application. I was able to create the tree structure with angularjs using directives, but i was not able to execute the jQuery. Please check my code http://plnkr.co/edit/JhG4lEiOX6uXYCyHZhOQ?p=preview and let me know what can i do to fix the issue. Below is the jQuery code that need to be executed.

$(function() {
    $("#tree").treeview({
      collapsed: false,
      animated: "medium",
      control: "#sidetreecontrol",
      persist: "location"
    });

    $thumbs = $('.thumbnail').click(function(e) {
      e.preventDefault();
      $thumbs.removeClass(classHighlight);
      $(this).addClass(classHighlight);
    });
})
5
  • you should avoid using jquery + angular, angular should fit your requirements somehow... Commented Feb 9, 2016 at 14:01
  • @Julo0sS I SO disagree.. Commented Feb 9, 2016 at 14:03
  • @Julo0sS that is not completely true. if you go at docs of angular.element, you can find that has basic implementation of jquery there. which would fail if you use some jquery library. Commented Feb 9, 2016 at 14:03
  • 1
    Possible duplicate of Convert jquery plugin into directive angular Commented Feb 9, 2016 at 14:03
  • @Jai This is not completely false... ^^ :P Commented Feb 9, 2016 at 14:04

3 Answers 3

1

You can make your own directive to wrap your jQuery plugin

http://bencentra.com/code/2015/09/29/jquery-plugins-angular-directives.html

Example:

var app = angular.module('MyApp', []);
app.controller('AppCtrl', ['$scope', function($scope) {
  $scope.sliderValue = 50;
}]);
app.directive('jqSlider', [function() {
  return {
    restrict: 'A',
    scope: {
      'model': '='
    },
    link: function(scope, elem, attrs) {
      $(elem).treeview({
       collapsed: false,
       animated: "medium",
       control: "#sidetreecontrol",
       persist: "location"
     });
    }
  };
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

I have updated my plunker, but i'm not able to understand how to use the following code $thumbs = $('.thumbnail').click(function(e) { e.preventDefault(); $thumbs.removeClass(classHighlight); $(this).addClass(classHighlight); });
0

It is true that is not recommended to use jQuery (a lot) when using AngularJS, but if you really need to here is a good example.

Also can check this related Stack Overflow question.

Comments

0

Updating my jQuery library solved the issue.

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.