0

I am new to AngularJs. I have a jquery code which I am planning to create a AngularJs directive. How to wrap the jquery code inside a AngularJs directive. I just want to use the same jquery code but want to call the code through the AngularJs directive. Please let me know if this is possible in AngularJs?

I am updating the post with two jquery sections that I am planning to create a directive:

     $('#pie-chart1').highcharts({
       title: {
       text: 'Exporting module is loaded but buttons are disabled'
       },
       xAxis: {
         categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
       },

       series: [{
       data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]        
      }],

      navigation: {
        buttonOptions: {
        enabled: false
      }
    }
  });
1

2 Answers 2

0

It is possible, but of course, there is no silver bullet. There are some jquery things that are really easy to port to angular, there are some that are harder. Generally speaking you can do everything, but the degree of difficulty can vary.

Edit: before going into doing what you are doing, you might wanna check out this:

https://github.com/pablojim/highcharts-ng

What you want to be achieved has already been achieved, so I'd recommend you don't reinvent the wheel or use their directive OR at least use it as a starting point.

2
  • Please view my updated question which contains the actual jquery code that I am planning to integrate with AngularJs Commented Apr 28, 2014 at 11:02
  • Yep, check the existing highcharts directive: github.com/pablojim/highcharts-ng Commented Apr 28, 2014 at 11:11
0

Inside the directive, you can use jQuery. In fact, if you have jQuery loaded before Angular.js, the element passed into the linking function is already a jQuery object.

app.directive("myDirective", function() {
    return {
        link: function(scope, element) {
            element.addClass("jquery"); // or other jQuery code
        }
    };
});

Note that if jQuery is not loaded before Angular.js, the element is wrapped in jqLite, a subset of jQuery that ships with Angular.js.

EDIT Aug 8, 2014:

In your question, I see you are using Highcharts. This edit probably comes a wee bit late now, but perhaps a quick look as a tiny app using Highcharts in a directive will give you some extra insight: https://bitbucket.org/mingos/pi-approximations

4
  • Please view my updated question which contains the actual jquery code that I am planning to integrate with AngularJs. Commented Apr 28, 2014 at 11:02
  • Your update does not introduce any relevant information that would require the answer to be updated as well. Commented Apr 28, 2014 at 11:04
  • From my understanding you manipulating the DOM using jquery in an angular directive is considered bad practice, as angular compiles the HTML on scope.digest(), and if your messing around with the DOM with changing it outside of a digest then you could cause crazy issues with your application Commented Aug 4, 2014 at 15:36
  • @britztopher the $digest loop needs to be triggered manually when you use async code, but simply adding a class is harmless. Where exactly the "harmless" line is is not within the scope of my answer. The OP asked for a way to wrap jQuery code in a directive and this is most certainly possible. In fact, I would very much prefer to wrap most of e.g. jQuery UI plugins in directives than use them directly in my controllers. As far as I'm concerned, a sane approach to what is OK and what is going too far is all one needs. Commented Aug 5, 2014 at 8:14

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.