Javascript 间歇性“jqLite 不支持通过选择器查找元素!” 使用 RequireJS 加载 AngularJS 时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26818605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intermittent "Looking up elements via selectors is not supported by jqLite!" when loading AngularJS with RequireJS
提问by Louis
I load AngularJS through RequireJS. Most of the time there is no problembut once in a while I get the error:
我通过 RequireJS 加载 AngularJS。大多数情况下没有问题,但偶尔我会收到错误消息:
Uncaught Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite!
未捕获的错误:[jqLite:nosel] jqLite 不支持通过选择器查找元素!
I know that everything is actually loading so the issue is not with RequireJS not finding the files.
我知道一切实际上都在加载,所以问题不在于 RequireJS 找不到文件。
Here is my RequireJS configuration:
这是我的 RequireJS 配置:
require.config({
baseUrl: 'lib/',
paths: {
jquery: 'external/jquery-2.1.1',
angular: 'external/angular',
},
shim: {
angular: {
exports: "angular"
},
},
});
Here is how I load RequireJS and kickstart the load:
这是我加载 RequireJS 并启动加载的方法:
<script type="text/javascript" src="lib/requirejs/require.js"></script>
<script type="text/javascript" src="requirejs-config.js"></script>
<script>
require(["start"]);
</script>
I'm using AngularJS 1.3.0 and RequireJS 2.1.15.
我正在使用 AngularJS 1.3.0 和 RequireJS 2.1.15。
采纳答案by Louis
Intermittent problems like this are usually due to a missing dependency somewhere.Most of the time your modules load in the correct order but that's just due to chance. Once in a while, luck is not on your side and you get undesirable results.
像这样的间歇性问题通常是由于某处缺少依赖关系。大多数情况下,您的模块以正确的顺序加载,但这只是偶然。偶尔,运气不在你身边,你会得到不想要的结果。
The issue is that AngularJS uses jQuery only optionally. If it is present, then AngularJS uses it. If it is absent, then AngularJS will use a jqLite library, bundled with AngularJS, that supports a subset of what jQuery can do. One important difference is that a statement like:
问题是 AngularJS 仅可选地使用 jQuery。如果它存在,那么 AngularJS 就会使用它。如果它不存在,那么 AngularJS 将使用一个 jqLite 库,与 AngularJS 捆绑在一起,它支持 jQuery 可以做的一个子集。一个重要的区别是像这样的语句:
angular.element('[ng-controller="something"]')
will work only if jQuery is available to AngularJS, and won't work if jqLite is used instead. If RequireJS loads AngularJS before jQuery, then AngularJS will be working without jQuery and the statement will fail.
仅当 jQuery 可用于 AngularJS 时才有效,如果使用 jqLite 则无效。如果 RequireJS 在 jQuery 之前加载 AngularJS,那么 AngularJS 将在没有 jQuery 的情况下工作并且语句将失败。
This can be fixed by changing your AngularJS shim to:
这可以通过将您的 AngularJS 垫片更改为:
shim: {
angular: {
deps: ['jquery'],
exports: "angular"
},
},
回答by ccpizza
If you do not want to depend on jQuery then, provided that you can enforce the browser version, instead of
如果您不想依赖 jQuery,那么只要您可以强制执行浏览器版本,而不是
angular.element('.mySelector');
use
用
angular.element(document.querySelector('.mySelector'));
See hereto find out which browsers support document.querySelector.
请参阅此处了解哪些浏览器支持 document.querySelector。
回答by eddiewould
For posterity - this error can also be caused by attempting to compile an empty template string (e.g. one which was dynamically generated):
对于后代 - 尝试编译空模板字符串(例如动态生成的字符串)也可能导致此错误:
var someTemplate = "";
$compile(someTemplate); // throws (with jQuery lite at least)

