2016-06-30 2 views
1

Ionic2 앱을 만들고 있습니다. e.x (http : www.example.com/test-view.html)에 외부 templateUrl을로드하려는 구성 요소가 있습니다.ionic2에서 외부 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); 
    } 
} 

답변

3

당신은 이런 식으로 작업을 수행 할 수 있습니다 : 당신이 뭔가 공상을 필요로하는 경우가 DomSanitizationService을 사용해야합니다 그래서 HTML을, 소독됩니다

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

참고. 그것에 대해 잊지 마세요 보안 위험이 워드 프로세서 (;

+0

내가 이렇게하면 XSS 오류가 발생합니다 : XMLHttpRequest는 http : //website/test-view1.html을로드 할 수 없습니다. 'Access-Control- Allow-Origin '헤더가 요청 된 리소스에 존재합니다.'http : // localhost : 8100 '의 출처가 액세스 할 수 없으므로 – ShadowVariable

+0

프록시를 설정해야한다는 것을 알았습니다! 감사합니다. D – ShadowVariable

+0

@ShadowVariable, can 프록시 설정 방법을 보여 주시겠습니까? –

관련 문제