从 AngularJS 控制器将 HTML 插入视图
是否可以在 AngularJS 控制器中创建 HTML 片段并将此 HTML 显示在视图中?
这是因为需要将不一致的 JSON blob 转换为 id: value
对的嵌套列表。因此,HTML 在控制器中创建,我现在希望显示它。
我已经创建了一个模型属性,但如果不打印 HTML,就无法在视图中呈现它。
更新
看来问题是由于将创建的 HTML 角度渲染为引号内的字符串而引起的。将尝试找到解决此问题的方法。
控制器示例:
var SomeController = function () {
this.customHtml = '<ul><li>render me please</li></ul>';
}
视图示例:
<div ng:bind="customHtml"></div>
给出:
<div>
"<ul><li>render me please</li></ul>"
</div>
Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view?
This comes from a requirement to turn an inconsistent JSON blob into a nested list of id: value
pairs. Therefore the HTML is created in the controller and I am now looking to display it.
I have created a model property, but cannot render this in the view without it just printing the HTML.
Update
It appears that the problem arises from angular rendering the created HTML as a string within quotes. Will attempt to find a way around this.
Example controller :
var SomeController = function () {
this.customHtml = '<ul><li>render me please</li></ul>';
}
Example view :
<div ng:bind="customHtml"></div>
Gives :
<div>
"<ul><li>render me please</li></ul>"
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
对于 Angular 1.x,在 HTML 中使用
ng-bind-html
:此时您将收到
attempting to use an unsafe value in a safe context
错误,因此您需要使用 ngSanitize 或 $sce 来解决这个问题。$sce
在控制器中使用
$sce.trustAsHtml()
来转换html字符串。ngSanitize
有 2 个步骤:
包含 angular-sanitize.min.js 资源,即:
在 js 文件(控制器或通常是 app.js)中,包含 ngSanitize,即:
For Angular 1.x, use
ng-bind-html
in the HTML:At this point you would get a
attempting to use an unsafe value in a safe context
error so you need to either use ngSanitize or $sce to resolve that.$sce
Use
$sce.trustAsHtml()
in the controller to convert the html string.ngSanitize
There are 2 steps:
include the angular-sanitize.min.js resource, i.e.:
In a js file (controller or usually app.js), include ngSanitize, i.e.:
您还可以像这样创建一个过滤器:
然后在视图中
注意:此过滤器信任传递给它的所有 html,并且如果将带有用户输入的变量传递给它,则可能会出现 XSS 漏洞。
You can also create a filter like so:
Then in the view
Note: This filter trusts any and all html passed to it, and could present an XSS vulnerability if variables with user input are passed to it.
Angular JS 在标签内显示 HTML
上面链接中提供的解决方案对我有用,该线程上的任何选项都不起作用。对于任何在 AngularJS 版本 1.2.9 中寻找相同内容的人,
这里有一份副本:
编辑:
这是设置:
JS 文件:
HTML 文件:
Angular JS shows HTML within the tag
The solution provided in the above link worked for me, none of the options on this thread did. For anyone looking for the same thing with AngularJS version 1.2.9
Here's a copy:
EDIT:
Here's the set up:
JS file:
HTML file:
幸运的是,您不需要任何花哨的过滤器或不安全的方法来避免该错误消息。这是以预期且安全的方式在视图中正确输出 HTML 标记的完整实现。
清理模块必须包含在 Angular 之后:
然后,必须加载该模块:
这将允许您在来自控制器、指令等的字符串中包含标记:
,在模板中,必须像这样输出:
最后 产生预期的输出:42 是答案。
Fortunately, you don't need any fancy filters or unsafe methods to avoid that error message. This is the complete implementation to properly output HTML markup in a view in the intended and safe way.
The sanitize module must be included after Angular:
Then, the module must be loaded:
This will allow you to include markup in a string from a controller, directive, etc:
Finally, in a template, it must be output like so:
Which will produce the expected output: 42 is the answer.
我今天尝试过,我发现的唯一方法是这个
I have tried today, the only way I found was this
<div ng-bind-html-unsafe="expression"></div>
ng-bind-html-unsafe
不再有效。这是最短的方法:
创建过滤器:
在您看来:
PS 此方法不需要您包含
ngSanitize
模块。ng-bind-html-unsafe
no longer works.This is the shortest way:
Create a filter:
And in your view:
P.S. This method doesn't require you to include the
ngSanitize
module.在 html 上
或
在控制器上
也适用于
$scope.comment.msg = $sce.trustAsHtml(html);
on html
OR
on controller
works also with
$scope.comment.msg = $sce.trustAsHtml(html);
我发现使用 ng-sanitize 不允许我在 html 中添加 ng-click 。
为了解决这个问题,我添加了一个指令。就像这样:
这是 HTML:
祝你好运。
I found that using ng-sanitize did not allow me to add ng-click in the html.
To solve this I added a directive. Like this:
And this is the HTML:
Good luck.
只需按照 Angular(v1.4) docs 使用 ngBindHtml 执行此操作,
请确保将 ngSanitize 包含在模块的依赖关系。
那么它应该可以正常工作。
Just did this using ngBindHtml by following angular(v1.4) docs,
Make sure you include ngSanitize in the module's dependencies.
Then it should work fine.
另一个解决方案,与 blrbr 非常相似,除了使用作用域属性之外是:
然后
注意,您可以将
element.append()
替换为element.replaceWith()
Another solution, very similar to blrbr's except using a scoped attribute is:
And then
Note you may replace
element.append()
withelement.replaceWith()
对于这个问题还有一个解决方案,即在 Angular 中创建新的属性或指令。
product-specs.html
app.js
index.html< /strong>
或
或
https://docs.angularjs.org/guide/directive
there is one more solution for this problem using creating new attribute or directives in angular.
product-specs.html
app.js
index.html
or
or
https://docs.angularjs.org/guide/directive
您还可以使用ng-include。
您可以使用“ng-show”来显示隐藏此模板数据。
you can also use ng-include.
you can use "ng-show" to show hide this template data.
这是解决方案,制作一个像这样的过滤器
,并将其作为过滤器应用到 ng-bind-html ,就像
感谢 Ruben Decrop
here is the solution make a filter like this
and apply this as a filter to the ng-bind-html like
and thank to Ruben Decrop
使用
and
为此,您需要包含 angular-sanitize.js,
例如在你的 html 文件中
Use
and
For that, you need to include
angular-sanitize.js
,for example in your html-file with
这是一个简单(且不安全)的
bind-as-html
指令,不需要ngSanitize
:请注意,如果绑定不受信任,这将引发安全问题内容。
像这样使用:
Here's a simple (and unsafe)
bind-as-html
directive, without the need forngSanitize
:Note that this will open up for security issues, if binding untrusted content.
Use like so:
使用管道在 Angular 4 模板中显示 html 的工作示例。
1.Clated Pipe escape-html.pipe.ts
`
`
2. 将管道注册到 app.module.ts
在模板中使用
getDivHtml() { //可以根据要求返回html}
请在关联的 component.ts 文件中添加适当的 getDivHtml 实现。
Working example with pipe to display html in template with Angular 4.
1.Crated Pipe escape-html.pipe.ts
`
`
2. Register pipe to app.module.ts
Use in your template
getDivHtml() { //can return html as per requirement}
Please add appropriate implementation for getDivHtml in associated component.ts file.
只需简单地使用
[innerHTML]
,如下所示:在您需要使用
ng-bind-html
之前...Just simple use
[innerHTML]
, like below:Before you needed to use
ng-bind-html
...