简体   繁体   English

AngularJS + Json:如何呈现html

[英]AngularJS + Json: How to render html

(I know this question was asked many times, but I believe my setup is different and so a new question needed to be asked with a different scenario) (我知道这个问题被问过很多次,但是我相信我的设置是不同的,因此需要针对不同的情况提出一个新的问题)

There are plenty of examples out there that shows how to Render HTML, but I can't seem to get this to work with any of the examples. 那里有很多显示如何渲染HTML的示例,但是我似乎无法将其与任何示例一起使用。 I'd like to render html the {{aboutlongs[0].description}} (this has <br /> tags that I would like to render as html) 我想将html {{aboutlongs[0].description}}呈现为HTML(具有<br />标签,我想将其呈现为html)

Here's the js: 这是js:

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

the HTML: 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>

Can anyone point me to the right direction? 谁能指出我正确的方向?

The Json file: 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."
    }]

Posts I've tried: 我尝试过的帖子:

if you want to render string to html i recommand to use $sce.trustAsHtml(html) . 如果要将字符串呈现为html,我建议使用$sce.trustAsHtml(html)

you can create a sample filter like this 您可以创建一个这样的示例过滤器

.filter('trustHtml',function($sce){
  return function(html){
    return $sce.trustAsHtml(html)
  }
})

call the filter like this inside ng-bind-html ng-bind-html这样调用过滤器

<div class="reason-content" ng-bind-html="aboutlongs[0].description | trustHtml" ></div>

Demo 演示版

 angular.module("app",[]) .controller("ctrl",function($scope){ $scope.aboutlongs = [{ "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." }] }) .filter('trustHtml',function($sce){ return function(html){ return $sce.trustAsHtml(html) } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div class="background-white p20 reasons" > <h6><b>About {{aboutlongs[0].name}}</b></h6> <div class="reason-content" ng-bind-html="aboutlongs[0].description | trustHtml" > </div> </div> </div> 

Have a look at this awesome article Angular Trust Filter 看看这篇很棒的文章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.") }]; }]) ; 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM