javascript - Angular 如何确定 jQuery 是否存在?

标签 javascript angularjs

因为我遇到了一个奇怪的情况,即 Controller 的链接函数中的 Angular 对象(如 element)最终成为一个 jQLite 对象,尽管 jQuery 确实存在,因为它被加载到内存中并被成功使用同一页面的其他地方。

Angular FAQ 在这个问题上相当含糊:

Yes, Angular can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, Angular falls back to its own implementation of the subset of jQuery that we call jQLite.

那么“现在”到底是什么意思?

最佳答案

如果首先包含 Angular.js,它将使用 jQuery,否则它将使用它自己的 jqLit​​e。如果你在 AngularJS 之后加载 jQuery,AngularJS 将自身附加到 jqLit​​e 但你仍然可以通过 $.jQuery 访问 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
    });

更改脚本标签的顺序可能不会经常发生,但如果您开始模块化代码库,它可能会发生。特别是,this issue has happened同时使用一些模块加载器,如 RequireJS。

关于javascript - Angular 如何确定 jQuery 是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216155/

相关文章:

angularjs - 如何在 View 中进行过滤

javascript - 我的 AngularJs 代码无法正常运行并出现此错误 - TypeError : Cannot read property '#<Object>' of undefined

javascript - Angular Js - 避免在 ng-repeat 中重复

javascript - 有没有办法检测自定义模块是否存在?

javascript - 发送消息后清除 &lt;textarea&gt; 时出现问题

javascript - 平滑的 anchor /ID 滚动不适用于标题的偏移量

javascript - 单击同一按钮提交并返回

javascript - jQuery:未捕获类型错误:对象#<Object>没有方法 'apply'

javascript - 涵盖没有函数传递给另一个函数的情况

javascript - 为什么 promise 在循环之外不起作用?