问题
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