在Angular中显示包含HTML标记的字符串

Fre*_*d A 2 html href angularjs angularjs-ng-href

我在SO中检查了答案,但找不到令人满意的答案.所以我在这里问:

我有一个字符串如下

var string = "With this you have agreed with the <a href='#'>rules and condition</a>"
Run Code Online (Sandbox Code Playgroud)

我需要将其呈现为字符串(对于文本部分)和HTML(对于HTML部分).

我如何在AngularJs中实现这一目标?我试过$compile但它对我不起作用,它在页面上输出看似缩小代码的块.

Saj*_*ran 5

您可以使用ng-bind-html执行此操作,

angular.module('myapp', ['ngSanitize'])
.controller('foo', function($scope) {
    $scope.bar = "With this you have agreed with the <a href='#'>rules and condition</a>";
});

<div ng-controller="foo">    
    <div ng-bind-html="bar"></div>    
</div>
Run Code Online (Sandbox Code Playgroud)

DEMO