Tracking up to HTML in AngularJS

I am using the Contentful API to download some content. It appears as a json object for my Node server, which passes it to my Angular interface. Content in json object contains raw markup text.

For example, an Angular call might look like this:

var id = "asdf76f7g68sd7g";
$http.get("/api/article/" + id).then(function(res){
  $scope.article = res.data;
});

The object for $ scope.article will look something like this:

$scope.article = {
  title: "Some title",
  content: "Lorem ipsum [dolor](http://google.com) sit amet, consectetur adipiscing elit. Integer iaculis turpis ut enim eleifend venenatis. Proin nec ante quis nulla porta finibus. Suspendisse at [mauris](http://google.com) nisi."
};

In my HTML, I would then display the content as follows:

<p>{{article.title}}</p>
<p>{{article.content}}</p>

The problem here is that markdown is not converted to HTML and displays how you see it in the object. How can I convert any markdown to HTML and display the results?

+4
source share
2 answers
+11

, markdown-js. ( , $sce, angular HTML - ):

var id = "asdf76f7g68sd7g";
$http.get("/api/article/" + id).then(function(res){
  var article = res.data;
  article.content = $sce.trustAsHtml(markdown.toHTML(article.content));
  $scope.article = article;
});

:

<div ng-bind-html="article.content"></div>
+2

Source: https://habr.com/ru/post/1584509/