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

前端之家收集整理的这篇文章主要介绍了在AngularJS中,包含在HTML模板中的任何内联JavaScript代码都不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在AngularJS中,包含在HTML模板中的任何内联JavaScript代码都不起作用。

例如:

main.html文件

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

和script.html文件

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

当我打开主页面时,我会发出一个警告消息,说“是”,但没有任何反应。我认为AngularJS中的一些安全限制是阻止内联脚本,但是我找不到任何解决方法

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

jQlite不支持脚本标签。 jQuery做,所以建议包括jQuery,如果你需要这个功能

来自Angular’s 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

猜你在找的Angularjs相关文章