0

How do I replace the following charachters "&#39" returned from $http data with " ' " ?

this is my code:

  $scope.searchInput = "";

  $scope.getResults = function(){
    $http.get('http://myapi'+ $scope.searchInput).
    success(function(data){

        $scope.myResults = data;

    });
  };

  $scope.getResults();

This is what the Json Object title looks like:

title: "'Mad Max' Review: 5 Reasons Why 'Road Warrior' Is Better Than ..." 

3 Answers 3

1

If i understood your question properly , u need to escape the $scope-data object using $sce (Strict Contextual Escaping) dependency $sce.trustAsHtml can be used for this , please refer this https://docs.angularjs.org/api/ng/service/$sce

     $scope.doDecode = function(title){
        return $sce.trustAsHtml(title);
     };

//html

       <h2 ng-bind-html="doDecode(title)"></h2>
Sign up to request clarification or add additional context in comments.

Comments

0

If you just need to unencode "&#39;" you could use:

 $scope.searchInput = "";

  $scope.getResults = function(){
    $http.get('http://myapi'+ $scope.searchInput).
    success(function(data){
        data.title.replace(/&#39;/g, "'");
        $scope.myResults = data;

    });
  };

  $scope.getResults();

2 Comments

when I try this i get the error: "Cannot read property 'replace' of undefined"
You suggested the return data looked like {title: "&#39;Mad...", title: "Jurasic World"} but if it was e.g. {movies: [{title: "&#39;Mad..."}, {title: "Jurasic World"}]} you would need to do adjust accordingly to e.g. data.movies[0].title.replace(/&#39/g, "'") - or iterate over each.
0

I just had to add this in my Html file: ng-bind-html="object.title"

   <div class="card" ng-repeat="object in Results" >
    <div class="item item-divider">
      {{object.visibleUrl}}
    </div>
    <div class="item item-text-wrap" ng-bind-html="object.title">
     {{object.title}}
    </div>
  </div>

Comments

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.