Angular2 create a component that displays the content of an external webpage

南楼画角 提交于 2019-12-23 12:44:14

问题


I need to create a component that displays the content of another webpage.

So for instance if I have the site of stackoverflow, I would like to create a component that does a http request and displays the content through my app. By the way, the external website is just django-rest-swagger and to access it, I need to include a header everytime I access it. So, when I make the request for the external site content I need to inlclude the header x-forwarded-host.

<div>
     <html> CONTENT OF EXTERNAL WEBSITE </html>
</div>

thank you


回答1:


@Component({
  selector: ...
  template: `<div [innerHTML]="fetchedHtml"></div>
})
export class ExternalHtml {
  constructor(http:Http) {
    var headers = new Headers();
    headers.append('x-forwarded-host', 'foo');
http.get('someUrl', {headers: headers}).subscribe(response => {
    this.fetchedHtml = response.json();
  }
}

See also

  • In RC.1 some styles can't be added using binding syntax

Alternatively you can also use an iframe to display the fetched HTML.




回答2:


You can display it as follows:

<div [innerHTML]="contentOfTheExternalWebsite">
</div>



回答3:


I replied to a similar question in this post

Loading External URL in Angular2 router-outlet

You can see my answer by following the above link



来源:https://stackoverflow.com/questions/38964499/angular2-create-a-component-that-displays-the-content-of-an-external-webpage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!