繁体   English   中英

如何在ionic2中加载外部html页面

[英]How to load external html page in ionic2

我正在创建一个Ionic2应用程序。 我有一个组件,我想将外部templateUrl加载到ex(http:www.example.com/test-view.html)。

如何将该html加载到@Component中的templateUrl变量中?

这是我到目前为止所拥有的。

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);
    }
}

你可以这样做:

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

请注意,html将被清理,所以如果你需要一些奇特的东西,你将不得不使用DomSanitizationService 不要忘记文档中的安全风险标志(;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM