加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:20:03 所属栏目:安全 来源:网络整理
导读:在AngularJS中,包含在HTML模板中的任何内联JavaScript代码都不起作用。 例如: main.html文件: div ng-include="'/templates/script.html'"/div 和script.html文件: script type="text/javascript" alert('yes');/script 当我打开主页面时,我会发出一个
在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读