下面你可以看到我的指令代码。
我的问题是:“我可以将 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。
关于 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'
};
});
关于javascript - 在 AngularJS 指令中使用 Jquery 是好主意还是坏主意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616009/