javascript - How to used html tag in json data? -
hi m using 1 json file in angualr js
{ "rohit":[ {"p":"i m john <strong> smith</strong> "}, {"p":" hi m rohit<span> azad</span> ."}, {"p" : "hi m rakesh singh "} ] }
if call json data called show result .
m john <strong> smith</strong> hi m rohit<span> azad</span> . hi m rakesh singh
why show html tag , how solve want used html tag in json format html tag .
the strings strings. interprets them html depends entirely on you're doing them. if use them html, html used; if use them text, won't be.
since you've said you're using angular, here's example of ng-bind-html
(interprets html; requires $sanitize
service) vs. ng-bind
(doesn't):
// json (i'm assuming comes somewhere, otherwise // literal in code, not json) var json = '{' + '"rohit":[' + '{"p":"i m john <strong> smith</strong> "},' + '{"p":" hi m rohit<span> azad</span> ."},' + '{"p" : "hi m rakesh singh "}' + ']' + '}'; // parse (i'm assuming comes somewhere, otherwise var data = json.parse(json); // use in angular (i don't "do" angular, i'm cherry-picking // value above , sticking on $scope) angular.module('example', ['ngsanitize']) .controller('examplecontroller', ['$scope', function($scope) { $scope.thetext = data.rohit[0].p; } ]);
<div ng-app="example"> <div ng-controller="examplecontroller"> <div>as html: <span ng-bind-html="thetext"></span> </div> <div>as text: <span ng-bind="thetext"></span> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-sanitize.min.js"></script>
Comments
Post a Comment