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/21336350/

相关文章:

javascript - 在背景上添加元素(圆圈)但在 html css js 中的文本后面

javascript - 清除jquery元素的dom缓存?

javascript - 页面上的多个 ng-app 指令

javascript - 对象中嵌套数组的 Angular ng-repeat 过滤器

javascript - 响应高度的视频纵横比可能吗?

html - 居中的内容和每边的线条填充剩余空间

javascript - 为什么 string.replace() 不适用于包含括号的字符串?

angularjs - AngularJS 的不同转换

javascript - 从 Photoshop Action 到 Photoshop 脚本?

javascript - 图片导航栏