【问题标题】:How to turn an html string from an array into html using angularjs ng-repeat?如何使用 angularjs ng-repeat 将数组中的 html 字符串转换为 html?
【发布时间】:2015-02-05 08:15:53
【问题描述】:

我有以下数组,其中每个项目的值都是一个 html 字符串。我怎样才能把这个字符串变成可执行的html,以便显示?

[
{html:'<button>name</button>'},
{html:'<table > <thead> <tr> <th>#</th> <th>UserName</th> <th>Time</th> <th>Comments</th> <th>Reply</th> <!-- <th>like or dislike</th> --> </tr> </thead> <tbody> <tr> <td>1,001</td> <td><a href="#" >Lorem</a></td> <td>2014-10-31</td> <td>ipsum</td> <td> <button type="submit" > <span ></span></button></td> </tr> <tr> <td>1,002</td> <td>amet</td> <td><a><span ></span></a></td> <td><a><span ></span></a></td> <td> <button type="submit" > <span ></span></button></td> </tr> <tr> <td>1,003</td> <td>Integer</td> <td>nec</td> <td>odio</td> <td> <button type="submit"> <span></span></button></td> </tr> <tr> <td>1,003</td> <td>libero</td> <td>Sed</td> <td>cursus</td> <td> <button type="submit" > <span></span></button></td> </tr> </tbody> </table>'},
{html:'<p>myhtml</p>'}
]

在此处查看代码笔:http://codepen.io/chriscruz/pen/YPwVmb

请参阅下面的 HTML

    <table class="table table-striped table-bordered table-hover" id="dataTables-example">
        <thead>
            <th>Email</th>
        </thead>
        <tbody>
            <tr ng-repeat="(id,item) in data">
                <td>{{item.html}}</td>
            </tr>
        </tbody>
    </table>


</body>

Javascript 和 Angular JS

    var app = angular.module("app", ["firebase"]);

    app.factory("TestArray", ["$firebase", function($firebase) {
        lst = [
        {html:'<button>name</button>'},
        {html:'<table > <thead> <tr> <th>#</th> <th>UserName</th> <th>Time</th> <th>Comments</th> <th>Reply</th> <!-- <th>like or dislike</th> --> </tr> </thead> <tbody> <tr> <td>1,001</td> <td><a href="#" >Lorem</a></td> <td>2014-10-31</td> <td>ipsum</td> <td> <button type="submit" > <span ></span></button></td> </tr> <tr> <td>1,002</td> <td>amet</td> <td><a><span ></span></a></td> <td><a><span ></span></a></td> <td> <button type="submit" > <span ></span></button></td> </tr> <tr> <td>1,003</td> <td>Integer</td> <td>nec</td> <td>odio</td> <td> <button type="submit"> <span></span></button></td> </tr> <tr> <td>1,003</td> <td>libero</td> <td>Sed</td> <td>cursus</td> <td> <button type="submit" > <span></span></button></td> </tr> </tbody> </table>'},
        {html:'<p>myhtml</p>'}
        ]
        return lst;
    }]);

    app.controller("ctrl", ["$scope","TestArray", function($scope,TestArray) {
        $scope.data = TestArray;
        //$scope.data = Emails;

    }]);

这是我希望看到的结果:http://codepen.io/chriscruz/pen/pvgwzw?editors=101

【问题讨论】:

    标签: javascript html arrays angularjs ng-repeat


    【解决方案1】:

    这里是Updated CodePen

    你需要添加angular-sanitize.js文件

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular-sanitize.js"></script>
    

    并在app 中导入ngSanitize 模块

    var app = angular.module("app", ["firebase",'ngSanitize']);
    

    然后你可以使用ng-bind-html指令绑定html

    <td ng-bind-html="item.html"></td>
    

    【讨论】:

    • 它似乎不适用于某些 html 代码。举个例子:codepen.io/chriscruz/pen/KwVqKq?editors=101。如果需要,我可以在数组形成过程中操作字符串 - 我可以做些什么来完成这项工作吗?
    • 请检查您的 html 是否杂乱无章。似乎您的 html 结构不合理。
    • 谢谢 - 我有一些清理工作要做。我使用python的Beautifulsoup来清理数组中的html并将所有引号替换为撇号。这似乎可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2015-12-26
    • 2020-08-17
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多