Javascript 检查angular是否已加载,并用最新版本覆盖它
我正在SharePoint 2013中开发一个非常复杂的功能,其中我需要将角度代码作为Web部件包含在页面中 有几种情况,例如,Javascript 检查angular是否已加载,并用最新版本覆盖它,javascript,angularjs,sharepoint,Javascript,Angularjs,Sharepoint,我正在SharePoint 2013中开发一个非常复杂的功能,其中我需要将角度代码作为Web部件包含在页面中 有几种情况,例如, 母版页(页眉、页脚和其他内容的共享布局)可能已经包含angularJS文件(也可以是旧版本) 当我们添加包含角度代码的web部件时,我也会在其中包含角度脚本引用,因为我们永远不知道母版页/共享布局是否包含角度引用 我试图重现类似的情况- -包含两次角度脚本。。第一个是旧的,第二个是最新的。代码无法工作,出现以下错误- WARNING: Tried to load an
WARNING: Tried to load angular more than once.
Uncaught TypeError: undefined is not a functionangular.js:26130 (anonymous function)
我想要实现的目标-
我想检查母版页是否已经引用Angular,即使已经引用了,也不要理会它,而是更喜欢我给出的引用,因为我的代码将是模块化的,并且依赖于我想要包含的脚本(最新的,很可能)
我知道这听起来很愚蠢,但SharePoint Web部件就是这样工作的。。我相信angularjs中会有类似
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;
}
把这个问题提出来
它可以帮助您检测角度传感器的存在。
甚至我也有一个类似的场景,我想知道如何找到angular版本并加载最近的版本
getAllElementsWithAttribute('ng-app');