-1

Here is my code in angular controller:

$scope.showClients='<table>';

for (var i = 0; i < data[0].nomBCL.length; i++)
{
    $scope.showClients+='<tr><td>'+data[0].nomBCL[i]+'</td>';
    $scope.showClients+='<td>'+data[0].tmsBCL[i]+'</td></tr>';

    ////   OR
    //$scope.showClients+='<tr><td>'+data[0].nomBCL[i][0]+'</td>';
    //$scope.showClients+='<td>'+data[0].nomBCL[i][1]+'</td></tr>';
}

$scope.showClients+='</table>';

angular ng-bind-html doesn't work

The other idea that i thinked of it to pass the data through $scope to the html page as 2d array and put 'for' or 'while' in a javascript, but neither this worked.

The data are from mysql database

  • Use `ng-repeat` instead. Don't mix templates and your controller. – Davin Tryon Jan 10 '15 at 12:28
  • There are rare cases when you have no other choice. In this case I believe you are right, mixing is a bad idea. But if you load some content, like an article or something, from your database via ajax it might be necessary to use ng-bind-html – Ziga Petek Jan 10 '15 at 12:33

1 Answers1

0

"does not work" is usually a bad description for a problem.

Do you get a blank result? In that case it might be possible that Angular doesn't want to show the html because it is considered unsafe.

You have to use the $sce service to make your html string safe:

First inject the service into your controller:

myApp.controller('myCtrl', ['$scope', '$sce', function($scope, $sce) {
   ...
}

Make your string html safe:

$scope.showClientsSafe = $sce.trustAsHtml($scope.showClients);

Now use "showClientsSafe" instead of "showClients".

I think you have to call the trustAsHtml function everytime you assign "showClients" to "showClientsSafe".

Ziga Petek
  • 3,953
  • 5
  • 33
  • 43