javascript - AngularJS + Json : How to render html

标签 javascript angularjs json

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

有很多示例展示了如何渲染 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;
        });
});

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>

谁能给我指出正确的方向吗?

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."
    }]

我尝试过的帖子:

最佳答案

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

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

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

ng-bind-html中调用过滤器

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

演示

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>

关于javascript - AngularJS + Json : How to render html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43317123/

相关文章:

javascript - JavaScript .includes() 方法的多个条件

javascript - innerHTML - 让我发疯

angularjs - Angular 项目的 package.json 中的 jshintConfig

javascript - 如何访问名称中有空格的 Json 对象?

asp.net - 在 Global.asax 中设置 session 变量会导致 AJAX 错误

javascript - 如何在 Rails 4 上获取 ajax 注释

javascript - 如何测试从 Angular 服务获取数据的 Angular Controller ?

angularjs - Angular ng 级和 Fontawesome 4 至 5

php - 如何使用 API?

python - 从 Python 中的 JSON 响应中提取数据并排除特定数据