检查 angular 是否已经加载并用最新版本覆盖它

Check if angular is already loaded and overwrite it with recent version

我正在 SharePoint 2013 中开发一项非常复杂的功能,我需要在其中将 angular 代码作为网页的 Web 部件包含在内。

有几种情况,

  1. Masterpage(页眉和页脚以及其他内容的共享布局)可能已经包含 angularJS 文件(也可以是旧版本)。

  2. 当我们添加带有 angular 代码的 Web 部件时,我还在其中包含 angular 脚本引用,因为我们永远不知道母版页/共享布局是否具有 angular参考与否。

我尝试重现类似的情况 -

-包含 Angular 脚本两次。第一个是旧的 Angular,第二个是最新的。代码无法使用以下错误 -

WARNING: Tried to load angular more than once.
Uncaught TypeError: undefined is not a functionangular.js:26130 (anonymous function)

我想达到的目标-

我想检查母版页是否已经引用了 Angular,即使它已经引用了,忽略它并更喜欢我提供的引用,因为我的代码将是模块化的并且依赖于我想要的脚本包括(最新的,最有可能)

我知道这听起来很愚蠢,但这就是它与 SharePoint Web 部件一起工作的方式。我相信在 Angular..

中会有类似 noConflict 的方法

要知道 angularjs 是否已经加载到页面上,请在任何标签上查找 ng-app 属性。

你可能会用到这个功能:

function getAllElementsWithAttribute(attribute)
{
  var matchingElements = [];
  var allElements = document.getElementsByTagName('*');
  for (var i = 0, n = allElements.length; i < n; i++)
  {
    if (allElements[i].getAttribute(attribute) !== null)
    {
      // Element exists with attribute. Add to array.
      matchingElements.push(allElements[i]);
    }
  }
  return matchingElements;
}

然后,

getAllElementsWithAttribute('ng-app');

参考这个 SO 问题

Can I get elements by attribute selector when querySelectorAll is not available without using libraries?

它可以帮助您检测 angular 的存在。 即使我有类似的情况,也想知道如何找到 angular 版本并加载最近的版本。