尝试执行 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/