我正在尝试在angular2应用中显示来自wordpress网站的数据,wordpress帖子的内容可以包含DOM元素,但我想渲染它而不是将其显示为文本。
我正在使用rest API V2插件来获取wordpress的帖子。
Home.ts
import {Component, NgFor} from 'angular2/angular2';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {RouterLink, ROUTER_DIRECTIVES, RouteConfig} from 'angular2/router';
@Component({
selector: 'home',
viewProviders: [HTTP_PROVIDERS],
templateUrl: './app/home/home.html',
directives: [NgFor, ROUTER_DIRECTIVES]
})
export class HomeCmp {
posts: Array<any>;
constructor(http: Http) {
http.get('http://localhost/wptest/wp-json/wp/v2/posts').subscribe(res => {
this.posts = <any>res.json();
});
}
}
<h1>I'm home page</h1>
<ul>
<li *ng-for="#post of posts" class="post-item">
<a [router-link]="['/Post', {id: post.id}]">
<h3 class="post-title">
{{post.title.rendered}}
</h3>
<p class="post-excerpt">
{{post.excerpt.rendered}}
</p>
<div class="post-thumbnail"></div>
</a>
</li>
</ul>
答案 0 :(得分:1)
<p class="post-excerpt" [innerHtml]="post.excerpt.rendered"></p>
Eric Martinez回答