1

I have JSON data in which there are some HTML tags as well. When I'm trying to show that data in a browser it also prints the HTML tags as it is without converting them to what they are supposed to show. See my code below.

My JSON data (ibmndata.php I'm converting PHP data to JSON):

[
   {
    "title": "<span class='data'>Lectures</span>",
    "content" : "lectures.jpg"
      },
      {
    "title": "<span class='data'>Case Study Analysis</span>",
    "content" : "case-study.jpg"
      },
      {
    "title": "<span class='data'>Industry Analysis Projects</span>",
    "content" : "industry-a.jpg"
      },{
    "title": "<span class='data'>Summer Internship Projects</span>",
    "content" : "summer-internship.jpg"
      }
]

My Angular data

Model (here I have included 'ngSanitize' package as I found in some other example)

var app = angular.module('ibmnApp', ['ngAnimate', 'ngRoute', 'ngSanitize', 'ui.bootstrap']);

Controller

app.controller('ibmnController', ['$scope', '$http', function($scope, $http)
{
    $http.get('ibmndata.php').success(function(data)
    {
        $scope.ibmnlist = data;
    });
}]);

HTML part

<div class="col-md-9" ng-controller="ibmnController">
   <div class="col-md-9" ng-repeat="item in ibmnlist">
      <p>{{item.title}}</p>
      <p ng-bind-html="item">{{item.content}}</p>
   </div>
</div>

I found a similar problem on web but its solution is not working for me. Please tell me what could be the problem in my code or suggest me a way to do this in AngularJs. Thanks.

1
  • Because {{}} is only for text Commented Jun 30, 2016 at 11:39

1 Answer 1

2

this is a working plunker :http://plnkr.co/edit/fZ5LnxMv5Ngnzyv6fsz2?p=preview

you have to add the "unsafe" filter :

angular.module('myApp').filter('unsafe', function ($sce) {
   return function (val) {
      if( (typeof val == 'string' || val instanceof String) ) {
         return $sce.trustAsHtml(val);
      }
   };
});

and in the view :

<div ng-controller="ibmnController">
   <div ng-repeat="item in ibmnlist">

      <p ng-bind-html="item.title | unsafe "></p>
      <p>{{item.content}}</p>
   </div>
</div>

BONUS : you are using success for $http who is depreceated, use then instead :

offical documentation :

// Simple GET request example:

$http({

 method: 'GET',

 url: '/someUrl'

 }).then(function successCallback(response) {

  // this callback will be called asynchronously

  // when the response is available

 }, function errorCallback(response) {

  // called asynchronously if an error occurs

  // or server returns response with an error status.

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

2 Comments

They are actually title and content. I have corrected that. I'll try your solution.
Thanks for the solution.

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.