Javascript 解码JSON HTML数据以显示在AngularJs中
我有JSON数据,其中也有一些HTML标记。当我试图在浏览器中显示数据时,它也会按原样打印HTML标记,而不会将它们转换为它们应该显示的内容。请参阅下面的代码 我的JSON数据(ibmndata.php我正在将php数据转换为JSON): 控制器Javascript 解码JSON HTML数据以显示在AngularJs中,javascript,html,angularjs,json,Javascript,Html,Angularjs,Json,我有JSON数据,其中也有一些HTML标记。当我试图在浏览器中显示数据时,它也会按原样打印HTML标记,而不会将它们转换为它们应该显示的内容。请参阅下面的代码 我的JSON数据(ibmndata.php我正在将php数据转换为JSON): 控制器 app.controller('ibmnController', ['$scope', '$http', function($scope, $http) { $http.get('ibmndata.php').success(function(
app.controller('ibmnController', ['$scope', '$http', function($scope, $http)
{
$http.get('ibmndata.php').success(function(data)
{
$scope.ibmnlist = data;
});
}]);
HTML部分
<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>
{{item.title}
{{item.content}
我在网上发现了一个类似的问题,但它的解决方案对我不起作用。请告诉我代码中可能存在的问题,或者建议我用AngularJs实现这一点的方法。谢谢。这是一个有效的打捞工具: 您必须添加“不安全”过滤器: 并且认为:
<div ng-controller="ibmnController">
<div ng-repeat="item in ibmnlist">
<p ng-bind-html="item.title | unsafe "></p>
<p>{{item.content}}</p>
</div>
</div>
它们实际上是标题和内容。我已经改正了。我会试试你的解决方案。谢谢你的解决方案。因为
{{}
仅用于文本
<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>
angular.module('myApp').filter('unsafe', function ($sce) {
return function (val) {
if( (typeof val == 'string' || val instanceof String) ) {
return $sce.trustAsHtml(val);
}
};
});
<div ng-controller="ibmnController">
<div ng-repeat="item in ibmnlist">
<p ng-bind-html="item.title | unsafe "></p>
<p>{{item.content}}</p>
</div>
</div>
$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.
});