Angular ng-repeat and $sce.trustAsHtml

Asked

Viewed 587 times

0

I’m developing a system where goes PHP in PDO, Angular with crud getting faster and picking up the list that comes from the Mysql database, the version of Angularjs is v1.5.8.

So I’m caught in a part $sce.trustAsHtml and I’ve looked this site as a reference. That’s basically what I want but it doesn’t work. I also saw this example which is in C# but no problem.

Code is the one without ng-repeat only that it is simple, I need that the list of data that comes from the base with HTML code is converted and does not present <b>oi</b> and yes hi.

Let me illustrate this better: the list comes from the database and converts to JSON getting:

{ nome: `<b>andré</b>`' }

Only it displays in View in HTML <b>andré</b>

my code in js controller looks like this:

var app = angular.module('myAngularApp',[]);   
app.controller('Controller',function($scope, $http, $sce){

$scope.getOldFour = function () {
    $http({
        method: 'GET',
        url: 'Controller/noticias-controller/read.php'           
    }).then(function successCallback(response) {

        for (var i = 0; i < response.data.length; i++) {
            response.data[i].records = $sce.trustAsHtml(response.data[i].records);
        }


        $scope.noticiasFour = response.data;
        //$scope.noticiasFour = response.data.records;
    });
  }    
});

and from what I saw in html I have to use this tag ng-bind-html

This is my HTML:

<ul class="news_tab" ng-init="getOldFour()">
  <li ng-repeat="OldFour in noticiasFour">                                                                
    <div class="media">
      <div class="media-left">
        <a class="news_img" href="#">
          <img class="media-object" src="App_Imagens/Noticias/Teste.jpg" alt="img">
        </a>
      </div>
      <div class="media-body">
        <div ng-bind-html="noticiasFourr"></div>
        <a href="#" ng-bind-html="">{{OldFour.descricao}}</a>
        <span class="feed_date">{{OldFour.data_postagem}}</span>
      </div>
    </div>
  </li>
</ul>

I’ve tried to <a href="#" ng-bind-html="OldFour.descricao"></a> and nothing

In PHP in the controller that will read and make the list already tried too html_entity_decode() and it didn’t work either.

To send to the database already used htmlentities() leaving the string inside the larger comic instead of going <b></b>

I’ve tried every way and it doesn’t work, I’ve searched several sites, does anyone have a solution? An experience similar to resolution ?

  • If I’m not mistaken, ng-bind-html is used like this: <a href="#" ng-bind-html="OldFour.descricao"></a> or just some test left'?

  • André, the problem is in show the interpreted HTML in the view or in save as HTML to database? Your question is completely confused.

  • Hi Neuber Oliveira I did not understand your question. jbueno is to show the HTML interpreted in the view

  • A good example of what I try to do is to say that the user inserts a news and this textarea takes bootstrap-wysiwyg he uses this technology and goes to the database <a>Andre</a> but when he comes back he does not change and makes a link and yes it appears in the html <a>Andre</a>, I’ve done everything for $sce.trustAsHtml() obey me but I can’t, I don’t know if I’m putting it in the wrong place or forgetting something.

  • @Andréaranda Posted an answer.

  • @jbueno saw your post and answered, the problem is that I could not reach the solution, I needed to take a few more doubts with you of angular

  • Put an example of your JSON that is returning so we can have an idea. Can copy and paste any return.

Show 2 more comments

1 answer

1

I can’t quite understand your problem, but it’s pretty easy to do what you need.

First of all it is necessary to have the module ngSanitize referenced, then just use the directive ng-bind-html with the HTML that needs to be shown.

angular.module('app', ['ngSanitize']);

angular.module('app').controller('mainController', mainControllerFn);

function mainControllerFn(){
  var ctrl = this;
  ctrl.bindHtml = '<b>Andre</b>';
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.23/angular-sanitize.min.js"></script>

<div ng-app="app">
  <div ng-controller="mainController as mainCtrl">
    {{mainCtrl.bindHtml}}
        
    <div ng-bind-html="mainCtrl.bindHtml">
      
    </div>
  </div>  
</div>

  • I understand, I tried to use the angular-Sanitize here but it did not solve. In mine when I tried to apply this to ng-repeat it gave this error Error: [$sce:itype] http://errors.angularjs.org/1.5.8/$sce/itype? P0=html . If I take the $sce.trustAsHtml() from $Scope.noticiasFour = $sce.trustAsHtml(Response.data.Records); it works again only if it has <b></b> in html which is what I don’t want.

  • I don’t think I know the correct way to apply $sce.trustAsHtml() in this command $Scope.noticiasFour = Response.data.Records; which works and behind the database.

  • Young, next. What is the return of your backend and what do you need to do with it? Your code is doing some kind of nonsense.

  • tah no, I will post here the code of the backend to facilitate. I will separate in MVC

  • 2

    No, you don’t need the backend code. You just need to explain what you want to do and how the return is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.