gpt4 book ai didi

javascript - 如何使用 angularjs ng-repeat 将数组中的 html 字符串转换为 html?

转载 作者:行者123 更新时间:2023-11-28 00:46:17 25 4
gpt4 key购买 nike

我有以下数组,其中每个项目的值都是一个 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

最佳答案

这是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指令绑定(bind)html

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

关于javascript - 如何使用 angularjs ng-repeat 将数组中的 html 字符串转换为 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27345048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com