强制AngularJS在使用多个版本的jQuery时使用特定版本的jQuery [英] Force AngularJS to use specific version of jQuery while using multiple versions of jQuery at once
问题描述
我继承了在noConflict模式下将几个版本的jQuery加载到页面中的项目。应用程序是巨大的,测试缺失,似乎每个前devloper没有cohones大到足以删除以前的库。所以结果是$引用了jquery版本1.4.2。
是否有任何方法/技巧可以告诉角度应该使用jQuery版本?类似
I inherited the project where several version of jQuery are loaded into the page in noConflict mode. Application is huge, tests are missing and it seems every former devloper did not have "cohones" big enough to remove previous library. So the result is that $ refers to jquery version 1.4.2.
Is there any way/trick to tell angular wich version of jQuery should be used? Something like
angular.jq = j182
angular.jq = j182
我知道我可以这样做
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="xxx/scripts/angular.js"></script>
<script>j182 = jQuery.noConflict();</script>
然后我有一种隐藏的依赖,我想避免。
but then I have kind of "hidden dependency" which I would like to avoid.
推荐答案
你是对的 $。noConflict()
允许您同时使用2个版本的jQuery(除非真的需要,否则应该避免使用)。
You're right that $.noConflict()
allows you to play with 2 versions of jQuery at once (which should be avoided unless REALLY necessary).
要使角度模块使用所需的版本,可以使用 ng-jq =whateverYouCallIt
其中 whateverYouCallIt
在窗口
。示例:
To make your angular module use the version you want, you can use ng-jq="whateverYouCallIt"
where whateverYouCallIt
is variable in window
. Example:
<script type="text/javascript" src="/js/jquery-1.9.0.min.js"></script>
<script type="text/javascript">window.j190 = jQuery.noConflict();</script>
<script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
console.log('globally available jQuery: ' + jQuery.fn.jquery);
console.log('j190: ' + window.j190.fn.jquery);
</script>
...
<div ng-app='myApp' ng-jq="j190" ng-controller='MyController' id="myAngularApp">
请注意,因为如果你需要访问 $ scope
从其他地方,您必须通过 ng-jq
中指定的相同jQuery获取它:
Just be careful, because if you need to access $scope
from somewhere else, you will have to obtain it via the same jQuery that has been specified in ng-jq
:
var myAnguarAppScope = window.j190('#myAngularApp').scope();
BTW,上面的 console.log
来电输出:
BTW, the above console.log
calls output:
全球可用jQuery:1.7.2
j190:1.9.0
globally available jQuery: 1.7.2
j190: 1.9.0
这篇关于强制AngularJS在使用多个版本的jQuery时使用特定版本的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!