Html不是'我没认出Angularjs吐出来的标签

Html isn't recognizing the tags that Angularjs spitting out

本文关键字:Angularjs 标签 不是 Html      更新时间:2023-09-26

角度

    $scope.name = "<h1>John Doe</h1>"

HTML

    <div>{{name}}</div>

输出:

    <h1>John Doe</h1>

如何使Html识别我在页面上吐出的标签。

正确的方法是:

$scope.name = 'John Doe';

HTML:

<div><h1>{{name}}</h1></div>

我不知道如果你真的不需要的话,你为什么要在变量中设置html标签

根据注释编辑:

如果你需要在模板中使用html标签,请在SO:AngularJS上看到这个答案:将html插入视图

我不打算重复这个答案,因为这个答案解释得很好。

试试这个

<h1 ng-bind-html-unsafe="name"></h1>

试试这个

<div ng-if="name" ng-bind-html="name"></div>
<b>
$scope.renderHtml = function(html_code)
{
    return $sce.trustAsHtml(html_code);
};
HTML:
<p ng-bind-html="renderHtml(value.button)"></p></b>