1

I have the following array where each item's value is a string that is html. How can I turn this string into executable html, so that it can be shown?

[
{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>'}
]

See codepen here: http://codepen.io/chriscruz/pen/YPwVmb

See below for 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 and 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;

    }]);

This is the desired result that I'd like to see: http://codepen.io/chriscruz/pen/pvgwzw?editors=101

1 Answer 1

1

Here is the Updated CodePen

you need to add angular-sanitize.js file

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

and import ngSanitize module in app

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

then you can bind the html using ng-bind-html directive

<td ng-bind-html="item.html"></td>
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't seem to work with some html code. Take for example: codepen.io/chriscruz/pen/KwVqKq?editors=101. I'm able to manipulate the string in the array formation process if need be - is there anything I could do to make this work?
please check your html is not a messy one. seems like your html is not well structured.
Thanks - I had some cleaning up to do. I used python's Beautifulsoup to clean up the html in the array and replaced all quotations with an apostrophe. This seems to do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.