例如:
main.html文件:
<div ng-include="'/templates/script.html'"></div>
和script.html文件:
<script type="text/javascript"> alert('yes'); </script>
当我打开主页面时,我会发出一个警告消息,说“是”,但没有任何反应。我认为AngularJS中的一些安全限制是阻止内联脚本,但是我找不到任何解决方法。
注意:我不使用jQuery或任何其他框架,只有AngularJS 1.2.7。
来自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.