Correct way to integrate jQuery plugins in AngularJS

前端 未结 2 1703
庸人自扰
庸人自扰 2020-11-22 13:47

I was wondering what is the correct way to integrate jQuery plugins into my angular app. I\'ve found several tutorials and screen-casts but they seem catered to a specific p

相关标签:
2条回答
  • 2020-11-22 14:15

    Yes, you are correct. If you are using a jQuery plugin, do not put the code in the controller. Instead create a directive and put the code that you would normally have inside the link function of the directive.

    There are a couple of points in the documentation that you could take a look at. You can find them here:
    Common Pitfalls

    Using controllers correctly

    Ensure that when you are referencing the script in your view, you refer it last - after the angularjs library, controllers, services and filters are referenced.

    EDIT: Rather than using $(element), you can make use of angular.element(element) when using AngularJS with jQuery

    0 讨论(0)
  • 2020-11-22 14:31

    i have alreay 2 situations where directives and services/factories didnt play well.

    the scenario is that i have (had) a directive that has dependency injection of a service, and from the directive i ask the service to make an ajax call (with $http).

    in the end, in both cases the ng-Repeat did not file at all, even when i gave the array an initial value.

    i even tried to make a directive with a controller and an isolated-scope

    only when i moved everything to a controller and it worked like magic.

    example about this here Initialising jQuery plugin (RoyalSlider) in Angular JS

    0 讨论(0)
提交回复
热议问题