【问题标题】:How to load external html page in ionic2如何在ionic2中加载外部html页面
【发布时间】:2016-11-03 05:33:30
【问题描述】:

我正在创建一个 Ionic2 应用程序。我有一个组件,我想将外部 templateUrl 加载到 e.x (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);
    }
}

【问题讨论】:

    标签: angular ionic2


    【解决方案1】:

    你可以这样做:

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

    请注意,html 将被清理,因此如果您需要一些花哨的内容,则必须使用 DomSanitizationService。不要忘记文档中的 Security Risk 标志 (;

    【讨论】:

    • 当我这样做时,我得到一个 XSS 错误:XMLHttpRequest 无法加载website/test-view1.html。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'localhost:8100'。
    • 没关系,我意识到我必须设置代理!非常感谢:D
    • @ShadowVariable,你能告诉我你是如何设置代理的吗?
    • @Sasxa 我遇到了这个问题 -> Property 'map' does not exist on type 'Observable&lt;Object&gt;'.ts(2339)
    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多