使用应运行的脚本插入 HTML [英] Insert HTML with scripts that should run

查看:20
本文介绍了使用应运行的脚本插入 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此应用程序的一部分从需要显示为实时 HTML 的服务器获取 HTML.使用 ng-bind-html-unsafe="expression" 指令很容易做到这一点(至少在 Angular 1.0.2 中).

One part of this app gets HTML back from the server that needs to be shown as live HTML. That's easily done with the ng-bind-html-unsafe="expression" directive (in Angular 1.0.2 at least).

然而,HTML 也有应该运行的 JavaScript,因为它定义了 HTML 中使用的函数,而使用指令似乎不会发生这种情况.

However, the HTML also has JavaScript that should run, since it defines functions used in the HTML, and that doesn't seem to happen using the directive.

是否有一些 Angular 风格的方法来做到这一点?还是我需要探索带外脚本加载技术?

Is there some Angular-style way to do that? Or do I need to explore out-of-band script loading techniques?

(请不要讨论我是否应该足够信任服务器来运行它发送给我的 java 脚本.我确实信任服务器,我知道风险,这是一种非常特殊的情况.)

(Please let's not discuss whether I should trust the server enough to run java-script it sends me. I do trust the server, I'm aware of the risks, and this is a very specialized situation.)

推荐答案

你需要 jQuery(在 Angular 之前加载它):

You need jQuery (load it before Angular):

"我们研究了在 jqlite 中支持 script 标签,但需要做什么才能获得跨浏览器支持涉及很多黑魔法.因此我们决定现在我们只推荐用户使用在这种特殊情况下,jquery 和 angular 一起使用"-https://groups.google.com/d/msg/angular/H4haaMePJU0/5seG803by5kJ

"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" -- https://groups.google.com/d/msg/angular/H4haaMePJU0/5seG803by5kJ

另见 AngularJS:如何进行角加载ng-include 中的脚本?

这篇关于使用应运行的脚本插入 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆