javascript 在 AngularJS 指令中使用 Jquery 好还是坏主意?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30616009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:25:55  来源:igfitidea点击:

Using Jquery inside AngularJS directive good or bad idea?

javascriptjqueryangularjsangularjs-directive

提问by Vesko Vujovic

Below you can see my code for the directive.

您可以在下面看到我的指令代码。

My question is: " Can i use jquery with directives? Is that a good idea? If not why? "

我的问题是:“我可以将 jquery 与指令一起使用吗?这是个好主意吗?如果不是,为什么?”

outsource.directive('dedicated', function(){

        return {
           restrict: 'E',
           link: function(scope, element, attribute){

                 $("#klik").click(function(){
                    alert('works');
                 });

           },

           replace: true,
           templateUrl: 'src/app/components/views/dedicated-prices.html'
        };
    });

P.s this code works.

Ps 此代码有效。

采纳答案by V31

You should not be using jquery as Angular itself has a lighter version for it known as jqlite.

您不应该使用 jquery,因为 Angular 本身有一个更轻的版本,称为 jqlite。

More documentation on JQLITE

更多关于JQLITE 的文档

So your directive should look like:

所以你的指令应该是这样的:

outsource.directive('dedicated', function(){

    return {
       restrict: 'E',
       link: function(scope, element, attribute){

             var elem = angular.element(document.querySelector('#klik'))
             angular.element(elem).triggerHandler('click');

       },

       replace: true,
       templateUrl: 'src/app/components/views/dedicated-prices.html'
    };
});

回答by Navaneeth

Simple Answer: YES(Simply refer jquery.js above Angular.js in HTML page. jqLite will be replaced by jQuery)

You would be using jQuery for DOM manipulation & there are many discussions going on this topic (whether to use or not in modern browsers).

One of the popular posts in the recentdays: http://lea.verou.me/2015/04/jquery-considered-harmful/

Despite everything, jQuery is still a very popular, highly used DOM library. And, it works with many modern UI frameworks seamlessly.

简单的答案:(在 HTML 页面中简单地将 jquery.js 引用到 Angular.js 之上。jqLit​​e 将被 jQuery 取代)

您将使用 jQuery 进行 DOM 操作,并且有很多关于这个主题的讨论(是否在现代浏览器)。

最近流行的帖子之一:http://lea.verou.me/2015/04/jquery-thinked-harmful/

尽管如此,jQuery 仍然是一个非常流行、使用率很高的 DOM 库。而且,它可以无缝地与许多现代 UI 框架配合使用。

回答by Stu

Interesting question. I've got some jquery with element selection in some directives/controllers in my codebase.

有趣的问题。我在我的代码库中的一些指令/控制器中有一些带有元素选择的 jquery。

I always feel dirty using it and only do it when I really need to, unfortunately it's almost always a time bomb and leads to me cursing myself a few months down the line and refactoring to use a more angulary method.

我总是觉得使用它很脏,只有在我真正需要的时候才这样做,不幸的是,它几乎总是一个定时炸弹,导致我在几个月后诅咒自己并重构以使用更有角度的方法。

Have one last look to see if there's a native angular way of doing what you're trying to do, you won't regret it!

最后看看是否有一种原生的角度方式来做你想做的事情,你不会后悔的!