我有一个
mysql数据库,存储我想要在我的页面上显示的文本值(文章的正文).我想存储,然后显示一些文本格式,但不知道如何使用角度.
我使用PHP PDO echo json_encode输出我的数据库查询结果.
所以我想我只是在我的文本中插入一些html标记(一些标题和段落标记),然后让浏览器对其进行格式化,但现在我的浏览器将html标记显示为文本.
是否有AngularJS过滤器可以轻松完成此操作?我通过文档搜索但无法找到类似的东西.
我真的不想为我在数据库中存储的每一篇文章编写html标记,但我不知道如何做到这一点.在此之前,我刚刚编写了一个自定义jQuery函数,用< br>代替了行制动器.等等.
这是我的代码:
HTML:
<div id="full-article" ng-repeat="fArticle in fullArticle" ng-show="!Feed" ng-hide="Feed"> <h1>{{ fArticle.title }}</h1> <h3>{{ fArticle.posted | date:"dd.MM.yyyy"}}</h3> <p>{{ fArticle.body }}</p> <button ng-click="backToFeed()">Back to articles</button> </div>
控制器:
'use strict'; angular.module('ptcAngularJsApp') .controller('ArticlesCtrl',function ($scope,$http) { // $scope.fullArticle = []; $scope.Feed = true; $scope.message = ''; $scope.findArticleByTitle = function(article) { $http.post('http://localhost/PTC_db_PHP/article_by_title.PHP',article.title). success(function(response) { $scope.fullArticle = response; $scope.Feed = false; }). //end http post response error(function(err) { console.log(err); }); //end http post error }; //end findArticleByTitle }); //end .controller
解决方法
Angular不允许你立即显示html,你必须说它是可信的,所以改变你的成功(确保你有依赖注入$sce):
$scope.fullArticle = response; for(var x = 0; x < $scope.fullArtcile.length; x++){ $scope.fullArticle[x].trustedBody = $sce.trustAsHtml($scope.fullArticle[x].body) }
然后制作你的html:
<p data-ng-bind-html="fArticle.trustedBody"></p>
你可以在这里阅读:https://docs.angularjs.org/api/ng/directive/ngBindHtml并查看以下答案:With ng-bind-html-unsafe removed,how do I inject HTML?