AngularJS + Json:如何渲染 html

soj*_*im2 1 javascript json angularjs

(我知道这个问题被问了很多次,但我相信我的设置不同,因此需要在不同的场景中提出一个新问题)

有很多示例展示了如何渲染 HTML,但我似乎无法使其与任何示例一起使用。我想渲染 html {{aboutlongs[0].description}}(这有<br />我想渲染为 html 的标签)

这是js:

App.controller('aboutLongCtrl', function ($scope, $http) {
    $http.get('test_data/ar_org.json')
        .then(function (res) {
            $scope.aboutlongs = res.data.aboutlong;
        });
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
    <h6><b>About {{aboutlongs[0].name}}</b></h6>
    <div class="reason-content" >
       {{aboutlongs[0].description}}
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向吗?

Json 文件:

"aboutlong": [{
        "name": "Women's March",
        "description": "The rhetoric of the past election cycle has insulted, demonized, and threatened many of us - immigrants of all statuses, Muslims and those of diverse religious faiths, people who identify as LGBTQIA, Native people, Black and Brown people, people with disabilities, survivors of sexual assault - and our communities are hurting and scared. We are confronted with the question of how to move forward in the face of national and international concern and fear.<br /><br />In the spirit of democracy and honoring the champions of human rights, dignity, and justice who have come before us, we join in diversity to show our presence in numbers too great to ignore. The Women's March on Washington will send a bold message to our new government on their first day in office, and to the world that women's rights are human rights. We stand together, recognizing that defending the most marginalized among us is defending all of us.<br /><br />We support the advocacy and resistance movements that reflect our multiple and intersecting identities. We call on all defenders of human rights to join us. This march is the first step towards unifying our communities, grounded in new relationships, to create change from the grassroots level up. We will not rest until women have parity and equity at all levels of leadership in society. We work peacefully while recognizing there is no true peace without justice and equity for all.<br /><br />Women's rights are human rights, regardless of a woman's race, ethnicity, religion, immigration status, sexual identity, gender expression, economic status, age or disability. We practice empathy with the intent to learn about the intersecting identities of each other. We will suspend our first judgement and do our best to lead without ego."
    }]
Run Code Online (Sandbox Code Playgroud)

我尝试过的帖子:

Sac*_*aka 5

如果你想将字符串渲染为 html,我建议使用$sce.trustAsHtml(html).

你可以像这样创建一个示例过滤器

.filter('trustHtml',function($sce){
  return function(html){
    return $sce.trustAsHtml(html)
  }
})
Run Code Online (Sandbox Code Playgroud)

在里面这样调用过滤器ng-bind-html

<div class="reason-content" ng-bind-html="aboutlongs[0].description | trustHtml" ></div>
Run Code Online (Sandbox Code Playgroud)

演示

.filter('trustHtml',function($sce){
  return function(html){
    return $sce.trustAsHtml(html)
  }
})
Run Code Online (Sandbox Code Playgroud)
<div class="reason-content" ng-bind-html="aboutlongs[0].description | trustHtml" ></div>
Run Code Online (Sandbox Code Playgroud)


Pig*_*all 5

看看这篇很棒的文章Angular Trust Filter

angular.module('myApp', [])
    .controller('aboutLongCtrl', ['$scope', '$sce', function($scope, $sce) {
      $scope.aboutlongs =  [{
        "name": "Women's March",
        "description": $sce.trustAsHtml("The rhetoric of the past election cycle has insulted, demonized, and threatened many of us - immigrants of all statuses, Muslims and those of diverse religious faiths, people who identify as LGBTQIA, Native people, Black and Brown people, people with disabilities, survivors of sexual assault - and our communities are hurting and scared. We are confronted with the question of how to move forward in the face of national and international concern and fear.<br /><br />In the spirit of democracy and honoring the champions of human rights, dignity, and justice who have come before us, we join in diversity to show our presence in numbers too great to ignore. The Women's March on Washington will send a bold message to our new government on their first day in office, and to the world that women's rights are human rights. We stand together, recognizing that defending the most marginalized among us is defending all of us.<br /><br />We support the advocacy and resistance movements that reflect our multiple and intersecting identities. We call on all defenders of human rights to join us. This march is the first step towards unifying our communities, grounded in new relationships, to create change from the grassroots level up. We will not rest until women have parity and equity at all levels of leadership in society. We work peacefully while recognizing there is no true peace without justice and equity for all.<br /><br />Women's rights are human rights, regardless of a woman's race, ethnicity, religion, immigration status, sexual identity, gender expression, economic status, age or disability. We practice empathy with the intent to learn about the intersecting identities of each other. We will suspend our first judgement and do our best to lead without ego.")
    }];
  }])
;
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
    <h6><b>About {{aboutlongs[0].name}}</b></h6>
    <div class="reason-content" ng-bind-html="aboutlongs[0].description">
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)