我正在使用SharePoint 2013中的一项非常复杂的功能,需要在页面中将角度代码作为Web部件包括在内。
有几种情况,例如
母版页(页眉和页脚以及其他内容的共享布局)可能已经包含angularJS文件(也可以是旧版本)。
当我们在其中添加带有角度代码的Web部件时,我也会在其中包含角度脚本参考,因为我们永远不知道母版页/共享布局是否具有角度参考。
我试图重现类似情况的内容-
-包含两次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');
提到这个问题
Can I get elements by attribute selector when querySelectorAll is not available without using libraries?
它可以帮助您检测角度的存在。
即使我也有类似的情况,并且想知道如何找到角度版本并加载最新版本。
关于javascript - 检查angular是否已经加载并用最新版本覆盖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28540274/