javascript - 在 AngularJS 中,HTML 模板中包含的任何内联 javascript 代码都不起作用

标签 javascript html angularjs

在 AngularJS 中,HTML 模板中包含的任何内联 JavaScript 代码都不起作用。

例如:

main.html 文件:

<div ng-include="'/templates/script.html'"></div>

和 script.html 文件:

<script type="text/javascript">
    alert('yes');
</script>

当我打开主页时,我期望收到一条警告消息,提示"is",但没有任何反应。我认为 AngularJS 中的一些安全限制正在阻止内联脚本,但我找不到任何解决方法。

注意:我不使用 jQuery 或任何其他框架,仅使用 AngularJS 1.2.7。

最佳答案

jQlite 不支持脚本标签。 jQuery 可以,因此如果您需要此功能,建议包含 jQuery。

来自 Angular 的 Igor Minar,位于 this discussion :

we looked into supporting script tags in jqlite, but what needs to be done to get a cross-browser support involves a lot of black magic. For this reason we decided that for now we are just going to recommend that users use jquery along with angular in this particular case. It doesn't make sense for us to rewrite one third of jquery to get this working in jqlite.

这是相关的 github 问题 jqLite should create elements in same way as jQuery在结束问题之前,Igor 总结道:

This is too much craziness for jqlite, so we are not going to do it. Instead we are going to document that if you want have script elements in ng:include or ng:view templates, you should use jquery.

demo plunker with jquery

关于javascript - 在 AngularJS 中,HTML 模板中包含的任何内联 javascript 代码都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30829118/

相关文章:

javascript - jQuery/Javascript 替换多次出现不起作用

javascript - rijndael-128 cbc javascript 不像 php mcrypt 那样返回正确

javascript - 如何使用 webpack-chain 将 appendTsSuffixTo 选项添加到 ts-loader?

html - 如何删除 HTML/CSS 中的曲线

javascript - 得到双 "onmouseover"Javascript

html - Flexbox 中内容居中的等高列

node.js - 浏览 Angular 时文件大小巨大

javascript - 将 Node.JS 代码片段转换为 Javascript(Google Apps 脚本)

javascript - $http超时是什么意思?

javascript - Angularjs ng-repeat 自定义过滤器会破坏 orderBy 过滤器吗?