在AngularJS指令中使用Jquery是好还是坏主意? [英] Using Jquery inside AngularJS directive good or bad idea?

查看:121
本文介绍了在AngularJS指令中使用Jquery是好还是坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below you can see my code for the directive.

我的问题是:我可以将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'
        };
    });

此代码有效.

推荐答案

您不应该使用jquery,因为Angular本身具有较轻的版本,称为jqlite.

You should not be using jquery as Angular itself has a lighter version for it known as 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'
    };
});

这篇关于在AngularJS指令中使用Jquery是好还是坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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