Skip to content

Commit ed0b91e

Browse files
Roy SivanRoy Sivan
authored andcommitted
HTML renderring
1 parent 33a617a commit ed0b91e

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

assets/js/angular_app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
var reang;
22
reang = angular.module('reang', ['ngResource'])
3+
.filter('to_trusted', ['$sce', function($sce) {
4+
return function(text) {
5+
return $sce.trustAsHtml(text);
6+
};
7+
}])
38
.factory('Posts', function($resource) {
49
return $resource(ajaxInfo.json_url + 'posts/:id', {
510
id: '@id'

assets/js/react_app.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ var $ = jQuery;
33

44
var Post = React.createClass({
55
render: function(){
6+
function post_content(html){ return {__html: html } }
67
return (
78
<article>
89
<h1>{this.props.post.title}</h1>
9-
{this.props.post.content}
10+
<div dangerouslySetInnerHTML={{__html: this.props.post.content}}></div>
1011
</article>
1112
)
1213
}
@@ -22,14 +23,13 @@ var App = React.createClass({
2223
dataType: 'json',
2324
method: 'GET',
2425
success: function(res){
25-
console.log( res );
2626
this.setState({data:res})
2727
}.bind(this)
2828
});
2929
},
3030

3131
getInitialState: function() {
32-
return { data: [{ title: 'test'}] }
32+
return { data: [{ title: 'test', content: '<p>test</p>'}] }
3333
},
3434

3535
componentDidMount: function() {

build/js/react_app.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ var $ = jQuery;
77
var Post = React.createClass({
88
displayName: "Post",
99
render: function () {
10+
var post_content = function (html) {
11+
return { __html: html };
12+
};
13+
1014
return React.createElement(
1115
"article",
1216
null,
@@ -15,7 +19,7 @@ var Post = React.createClass({
1519
null,
1620
this.props.post.title
1721
),
18-
this.props.post.content
22+
React.createElement("div", { dangerouslySetInnerHTML: { __html: this.props.post.content } })
1923
);
2024
}
2125
});
@@ -32,14 +36,13 @@ var App = React.createClass({
3236
dataType: "json",
3337
method: "GET",
3438
success: (function (res) {
35-
console.log(res);
3639
this.setState({ data: res });
3740
}).bind(this)
3841
});
3942
},
4043

4144
getInitialState: function () {
42-
return { data: [{ title: "test" }] };
45+
return { data: [{ title: "test", content: "<p>test</p>" }] };
4346
},
4447

4548
componentDidMount: function () {

build/js/scripts.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
var reang;
22
reang = angular.module('reang', ['ngResource'])
3+
.filter('to_trusted', ['$sce', function($sce) {
4+
return function(text) {
5+
return $sce.trustAsHtml(text);
6+
};
7+
}])
38
.factory('Posts', function($resource) {
49
return $resource(ajaxInfo.json_url + 'posts/:id', {
510
id: '@id'

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Angular
77
<article ng-repeat="post in posts">
88
<h1>{{post.title}}</h1>
9-
{{post.content}}
9+
<div ng-bind-html="post.content | to_trusted"></div>
1010
</article>
1111
</div>
1212
<div class="col-sm-6">

0 commit comments

Comments
 (0)