Search code examples
javascriptjqueryhtmlangularjsonsen-ui

i want to pass html code from angularjs to onsen page or pass an array to js in onsen page


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


Solution

  • "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".