在AngularJS指令中使用Jquery有什么好主意?

Ves*_*vic 11 javascript jquery angularjs angularjs-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'
        };
    });
Run Code Online (Sandbox Code Playgroud)

这个代码有效.

V31*_*V31 6

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

有关JQLITE的更多文档

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

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'
    };
});
Run Code Online (Sandbox Code Playgroud)