集成在AngularJS jQuery插件的正确方法 [英] Correct way to integrate jQuery plugins in AngularJS

查看:171
本文介绍了集成在AngularJS jQuery插件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是jQuery插件集成到我的角度应用的正确方法。我已经发现了几个教程和屏幕的演员,但他们似乎迎合了特定的插件。

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 plugin.

例如:
<一href=\"http://amitgharat.word$p$pss.com/2013/02/03/an-approach-to-use-jquery-plugins-with-angularjs/\">http://amitgharat.word$p$pss.com/2013/02/03/an-approach-to-use-jquery-plugins-with-angularjs/
http://www.youtube.com/watch?v=8ozyXwLzFYs

我应该创建一个指令,像这样 -

Should I create a directive like so -

App.directive('directiveName', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            $(element).'pluginActivationFunction'(scope.$eval(attrs.directiveName));
        }
    };
}); 

然后在HTML调用脚本和指令?

And then in the html call the script and the directive?

<div directiveName ></div>
<script type="text/javascript" src="pluginName.js"></script>

由于提前

推荐答案

是的,你是正确的。如果您使用的是jQuery插件,不要把code控制器。相反,创建一个指令,把code,你通常有内部链接指令的功能。

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

使用控制器正确

确保当你引用视图中的脚本,你是指它最后 - 在angularjs库后,控制器,服务和过滤器引用

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.

编辑:与其使用 $(元素),你可以使用 angular.element的(元素)使用与jQuery AngularJS时

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

这篇关于集成在AngularJS jQuery插件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆