angular如何确定jQuery是否存在

How does angular determine whether jQuery is present?

本文关键字:是否 存在 jQuery 何确定 angular      更新时间:2023-09-26

因为我遇到了一种奇怪的情况,即控制器链接函数中的角度对象(如element)最终成为jQLite对象,尽管jQuery肯定存在于内存中,并在同一页面的其他地方成功使用。

Angular常见问题解答在这个问题上相当模糊:

是的,Angular可以在应用程序启动时使用jQuery。如果您的脚本路径中没有jQuery,Angular将返回到它自己的jQuery子集实现,我们称之为jQLite。

那么"present"到底是什么意思呢?

Angular.js如果首先包含jQuery,就会使用jQuery,否则就会使用自己的jqLite。如果在AngularJS之后加载jQuery,AngularJS会将自己附加到jqLite,但您仍然可以通过$访问jQuery。

请参阅下面angular.js用于确定是否加载jquery的代码:

  // bind to jQuery if present;
  jQuery = window.jQuery;
  // Use jQuery if it exists with proper functionality, otherwise default to us.
  // Angular 1.2+ requires jQuery 1.7+ for on()/off() support.
  // Angular 1.3+ technically requires at least jQuery 2.1+ but it may work with older
  // versions. It will not work for sure with jQuery <1.7, though.
  if (jQuery && jQuery.fn.on) {
    jqLite = jQuery;
    extend(jQuery.fn, {
      scope: JQLitePrototype.scope,
      isolateScope: JQLitePrototype.isolateScope,
      controller: JQLitePrototype.controller,
      injector: JQLitePrototype.injector,
      inheritedData: JQLitePrototype.inheritedData
    });

更改脚本标记的顺序可能不会经常发生,但如果您开始对代码库进行模块化,则可能会发生这种情况。特别是,在使用一些模块加载程序(如RequireJS)时出现了此问题。