How to load external html page in ionic2

2019-02-20 00:51发布

I am creating an Ionic2 app. I have a component that I would like to load an external templateUrl into e.x (http:www.example.com/test-view.html).

How do I go about loading that html into the templateUrl variable in @Component?

Here's what I have so far.

import { Component, DynamicComponentLoader, ViewContainerRef,
    ViewChild } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { Http, Headers, RequestOptions } from '@angular/http';


@Component({
    //templateUrl: 'build/pages/hello-ionic/hello-ionic.html'
    template: `<div [innerHTML]="myVal"></div>`,
    selector: 'HelloIonicPage'
})
export class HelloIonicPage {

    myVal: any;
    viewLink: any;
    viewHtml: any;

    constructor(private http: Http) {


        let headers = new Headers({ 'Content-Type': 'text/html' });
        let options = new RequestOptions({ headers: headers });

        this.viewHtml = this.http.get('http://example.net//test-view1.html');

        this.myVal = this.viewLink
        console.log(this.myVal);
    }
}

1条回答
冷血范
2楼-- · 2019-02-20 01:11

You can do it like this:

this.http
  .get('http://example.net//test-view1.html')
  .map(response => response.text())
  .subscribe(html => myVal = html);

Note that html will be sanitized, so if you need something fancy you'll have to use DomSanitizationService. Don't forget about it's Security Risk flag in the docs (;

查看更多
登录 后发表回答