javascript - 1.9.1 与 1.8.3 中的 Angular.js/jQuery html 字符串解析

标签 javascript jquery angularjs jquery-1.9 jquery-1.8

尝试执行 angular.element(stringWithHtmlStructure); 会导致

Error: Syntax error, unrecognized expression: <div id="foo">bar</div>

在 jquery 1.9.1 中,但是它在 jquery 1.8.3 中有效

这是一个问题还是一个功能?也许是安全的?我该如何处理?

1.8.3 示例 http://plnkr.co/edit/ZYYKIixcYzOL46hq0puS?p=preview 1.9.1 例子http://plnkr.co/edit/ETmQMY8KoCP0gaKdSrMD?p=preview

这绝对不是任何 Angular 问题,做 $(stringWithHtmlStructure) 一样失败,正如你在这里看到的 http://plnkr.co/edit/FqvOKZ62t0IJ57cT05ew?p=preview

最佳答案

问题肯定是jquery 1.9.1

HTML strings with leading whitespace: jQuery 1.9 restricts the strings processed by $() for security reasons. Although we recommend you use $.parseHTML() to process arbitrary HTML like templates, the 1.1.0 version of the Migrate plugin restores the old behavior.

处理它的方法是使用任何类型的 trim 函数,无论是 jquery 的 $.trim 还是下划线 trim ,甚至是你自己的。

请注意,只有在尝试使用 $templateCache 中的模板时才会出现此问题,如果您在指令上使用模板或 templateUrl attr 它工作正常,我猜 Angular 内部会对其进行 trim 。

关于javascript - 1.9.1 与 1.8.3 中的 Angular.js/jQuery html 字符串解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16122139/

相关文章:

javascript - 调用相同 "class"的另一个方法,但 JavaScript 中的上下文不同?

Javascript,将文本添加到具有现有文本节点的元素

jquery - 为什么 jquery 上的 for 循环在 thymeleaf 上不起作用?

javascript - 仅在选中复选框时包含/启用表单字段

angularjs - Angular-ui-route 使用 angularJS 工厂解析

javascript - Angular $route.reload() 没有重新加载 Controller

javascript - 最佳实践 : Should I use ng-switch for this?

javascript - javascript 中的 "++"和 "+="有区别吗?

javascript - 在 Rails + Webpack 应用程序中包含 jQuery

javascript - Angular-ui/ui-router 嵌套状态不继承自定义数据