Javascript Angularjs 用 html 显示值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37468907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:21:55  来源:igfitidea点击:

Angularjs display value with html

javascripthtmlasp.netangularjs

提问by A191919

How to display Html from value? Like @Html.Raw() in ASP.NET. Because in current example i get only text with tags.

如何从 显示 Html value?就像 ASP.NET 中的 @Html.Raw()。因为在当前示例中,我只得到带有标签的文本。

JS:

JS

var app = angular.module('Demo', []);
app.controller('TestCtrl', function ($scope) {
    $scope.value = '<p>List</p><ul><li>Test 1</li><li>Test 2</li><li>Test 3</li><li>Test 4</li></ul>'
});

view:

观点

<div ng-controller="TestCtrl">
    {{value}}
</div>

回答by Simon Schüpbach

use

<div ng-bind-html="value"></div>

instead of

代替

{{value}}

you have to use $sceprovider to process your code to get a safe context.

您必须使用$sceprovider 来处理您的代码以获得安全的上下文。

$scope.value= $sce.trustAsHtml("yourHtmlCode");

回答by Juan Techera

You should use the ng-bind-html directive, you can read more here

你应该使用 ng-bind-html 指令,你可以在这里阅读更多

<div ng-controller="TestCtrl" ng-bind-html="{value}">