【发布时间】:2017-07-10 07:27:29
【问题描述】:
我正在显示一个字符串,其中包含来自 JSON 的 HTML 代码。 请检查第二个对象中的“名称”属性。
但是,当我使用“ng-bind-html-unsafe”时,它不会显示任何内容。
我也使用过 ngSanitize。
你能帮我弄清楚我的代码有什么问题吗?
angular.module('myApp', ['ngSanitize']).controller('myCtrl', ['$scope', '$sce', function($scope, $sce){
$scope.card = [{
Name: "New Year Celebration",
Description: "",
Venue: "",
StartDate: "Fri Dec 29 2017 23:30:00 GMT+0530",
EndDate: "Sat Dec 30 2017 00:30:00 GMT+0530",
EventID: "1"
}, {
Name: "<P>25th Anniversary Celebration</P>",
Description: "25th Anniversary Celebration of organization",
Venue: "Auditorium",
StartDate: "Wed May 31 2017 17:30:00 GMT+0530",
EndDate: "Wed May 31 2017 20:30:00 GMT+0530",
EventID: "2"
}, {
Name: "Annual Day",
Description: "",
Venue: "",
StartDate: "Fri Oct 13 2017 14:30:00 GMT+0530",
EndDate: "Fri Oct 13 2017 17:30:00 GMT+0530",
EventID: "3"
}];
$scope.trustAsHtml = function(html) {
return $sce.trustAsHtml(html);
}
$scope.add = function(eventObj) {
$scope.eventID= this.eventObj.EventID;
$scope.startDate= this.eventObj.StartDate;
$scope.endDate= this.eventObj.EndDate;
$scope.venue= this.eventObj.Venue;
$scope.subject= this.eventObj.Name;
$scope.result= this.eventObj.Description;
//console.log(this);
$scope.icsMSG = "BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\nDTSTART:" + $scope.startDate +"\nDTEND:" + $scope.endDate +"\nLOCATION:" + $scope.venue + "\nSUMMARY:" + $scope.subject + "\nDESCRIPTION:"+ $scope.result +"\nEND:VEVENT\nEND:VCALENDAR";
window.open("data:text/calendar;charset=utf8," + escape($scope.icsMSG),"_self");
};
}]);
.event {
height: 150px;
width: 250px;
border: 1px solid lightgrey;
background-color: skyblue;
margin: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.5/angular-sanitize.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="eventObj in card" class="event">
Subject: <span ng-bind-html="trustAsHtml(eventObj.Name)"></span>
<br /><br />
Venue:<span>{{eventObj.Venue}}</span>
<br /><br />
Date:<span>{{eventObj.StartDate | date:'fullDate'}}</span>
<br /><br />
<button ng-click="add(eventObj.EventID)">Add to Outlook</button>
</div>
</div>
【问题讨论】:
-
告诉我们您的网络控制台告诉您什么错误。类似于它被阻止的地方。
-
哦,别忘了禁用任何广告拦截器,否则它将无法加载
-
我更新了任何答案。这是工作演示。 plnkr.co/edit/sFhaSJ2Ir9PYUObdtcnj?p=preview
-
@sunny 更新的答案是否解决了问题?
标签: angularjs json object ng-bind-html ngsanitize