javascript Angularjs 和 jQuery $.noConflict()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18003662/
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-27 10:25:26  来源:igfitidea点击:

Angularjs and jQuery $.noConflict()

javascriptdomangularjsjquery

提问by nullVoid

Having some issues implementing any of the methods for noConflict. I'm not sure what is going on, the jQuery CDN loads before the Angularjs CDN also the file in which I try to use Jquery loads before any Angularjs files (controllers etc).

在实现 noConflict 的任何方法时遇到一些问题。我不确定发生了什么,jQuery CDN 在 Angularjs CDN 之前加载,还有我尝试在任何 Angularjs 文件(控制器等)之前使用 Jquery 加载的文件。

No jQuery functions are working when trying to implement this.

尝试实现此功能时,没有 jQuery 函数在工作。

Anyone know what is going on?

有谁知道发生了什么?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="../public/JS.js"></script>
<script src="../public/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script type="text/javascript" src="../public/javascript/comsumer.js"></script>
<script type="text/javascript" src="../public/javascript/controllers.js"></script>
<script type="text/javascript" src="../public/javascript/services.js"></script>
<script type="text/javascript" src="../public/javascript/directives.js"></script>

That is the order in which the files are called.

这就是调用文件的顺序。

Thanks in advance for any help you can offer.

预先感谢您提供的任何帮助。

Code that isn't working - all noConflict

不工作的代码 - 所有 noConflict

<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {
  // Code that uses jQuery's $ can follow here.
  });
 // Code that uses other library's $ can follow here.
</script>

Also

jQuery.noConflict();
 jQuery(".test").hide();

Also

jQuery.noConflict();
 (function($) {
   $(function() {
     // more code using $ as alias to jQuery
      $(".test").hide();
   });
})(jQuery);
  // other code using $ as an alias to the other library

Also

 var j = jQuery.noConflict();
   j(".test").hide();

lastly

最后

var dom = {};
dom.query = jQuery.noConflict(true);
   dom.query(".test").hide();

none seem to work, I have put them in my JS file, none jQuery function are triggering

似乎没有工作,我把它们放在我的 JS 文件中,没有 jQuery 函数被触发

采纳答案by nullVoid

Answer was staring me right in the face. Issues being that angularjs SPA 'injected' the view that had the DOM element I wanted to use jQuery on, so still using .noConflict(), I had to use something like this.

答案正盯着我的脸。问题是 angularjs SPA“注入”了具有我想在其上使用 jQuery 的 DOM 元素的视图,因此仍然使用 .noConflict(),我不得不使用类似的方法。

The Global

全球

<script type="text/javascript">
     var j = $.noConflict();
</script>

this in JS file

这个在 JS 文件中

j(document).off("click",".jtest",testclick ).on("click", ".jtest",testclick);

Jquery only sees the current DOM when loaded, so with it being injected using the above solution worked.

Jquery 仅在加载时看到当前 DOM,因此使用上述解决方案注入它是有效的。

The detail of the question should have been better, my bad.

问题的细节应该更好,我的不好。