1

I have html theat I want to use in angularjs controller controller. My problem is to escape the html in the angularjs controller. Here is my snippet

function($scope) {
     $scope.data=  <table class="table">
            <thead>
              <tr>
                <th>#</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Username</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>1</td>
                <td>$rootScope.firstname</td>
                <td>$rootScope.lastname</td>
                <td>@mdo</td>
              </tr>
              <tr>
                <td>2</td>
                <td>$rootScope.firstname1</td>
                <td>$rootScope.lastname1</td>
                <td>@fat</td>
              </tr>
              <tr>
                <td>3</td>
                <td>$rootScope.firstname2</td>
                <td>$rootScope.lastname2</td>
                <td>@twitter</td>
              </tr>
            </tbody>
          </table>;

how can I pass the table html and the root scope data into scope.data variable

1
  • Firstly - will be better if you will store you data as array and not as separated fields. This will give you a chance to access data from root scope only once and set them to scope. Like: $scope.userData = $rootScope.userData and then you will be able to use ng repeat for this cells and you will no need to use html in controller. Commented Nov 12, 2018 at 9:33

2 Answers 2

1

Just concatenate $rootScope fields then append it to any html div element

  function($scope) {
     $scope.data= '<table class="table">
            <thead>
              <tr>
                <th>#</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Username</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>1</td>
                <td>'+$rootScope.firstname+'</td>
                <td>'+$rootScope.lastname+'</td>
                <td>@mdo</td>
              </tr>
              <tr>
                <td>2</td>
                <td>'+$rootScope.firstname1+'</td>
                <td>'+$rootScope.lastname1+'</td>
                <td>@fat</td>
              </tr>
              <tr>
                <td>3</td>
                <td>'+$rootScope.firstname2+'</td>
                <td>'+$rootScope.lastname2+'</td>
                <td>@twitter</td>
              </tr>
            </tbody>
          </table>';

EDIT:

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

app.controller("TestController", function($scope, $rootScope) {
  
$rootScope.firstname="firstname";
$rootScope.lastname="lastname";
$rootScope.firstname1="firstname1";
$rootScope.lastname1="lastname1";
$rootScope.firstname2="firstname2";
$rootScope.lastname2="lastname2";

     $scope.data= '<table class="table"> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr></thead> <tbody> <tr> <td>1</td><td>'+$rootScope.firstname+'</td><td>'+$rootScope.lastname+'</td><td>@mdo</td></tr><tr> <td>2</td><td>'+$rootScope.firstname1+'</td><td>'+$rootScope.lastname1+'</td><td>@fat</td></tr><tr> <td>3</td><td>'+$rootScope.firstname2+'</td><td>'+$rootScope.lastname2+'</td><td>@twitter</td></tr></tbody> </table>';
     $("#data_table").append($scope.data);
          
          
          });
<html  ng-app="testProject">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>


<div  ng-controller="TestController" id="data_table"></div>


</html>

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting unxepected token at this line $scope.data= '<table class="table">
Uncaught SyntaxError: Invalid or unexpected token
0

Save it as string like this:

 $scope.data='<html><div></div></html>'

Then when you want to render it use ng-bind-html like this:

 <div>
   <p ng-bind-html="data"></p>
 </div>

1 Comment

please include the entire html used

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.