Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular

iframe

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

like image 371
Miguel Rosales Avatar asked Aug 15 '16 23:08

Miguel Rosales


People also ask

How can I use one component HTML in another component in angular 8?

if you want to insert HTML elements or other components in a component, then you do that using the concept of content projection. In Angular, you achieve content projection using < ng-content >< /ng-content >. You can make reusable components and scalable applications by properly using content projection.

What is the command to create component in angular?

To create a component using the Angular CLI: From a terminal window, navigate to the directory containing your application. Run the ng generate component <component-name> command, where <component-name> is the name of your new component.

What is @component in angular?

Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components. Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated for a given element in a template.


1 Answers

@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.

like image 172
Günter Zöchbauer Avatar answered Dec 15 '22 01:12

Günter Zöchbauer