http.get()
メソッドでHTML
ページを読み込み、このページの内容をdivタグに追加します。あなたが見る、このメソッドは、変数に応答を入れて、正規表現で文字列を解析 [OK]を、次の私は、良い本角2 innerHTMLからスクリプトタグを実行する方法
<div [innerHTML]="response | safe"></div>
のように、DIVに追加するにはどうすればよい
getRequestToAssignPage (param: string) : any {
return this.$http.get(param)
.map((res: Response) => {
this.status = res;
return res.text();
})
.toPromise()
.then(response => {
let restr: string = response;
restr = restr.replace(/(<head[^>]*)(?:[^])*?\/head>/ig, '')
.replace(/(<(\/?)body([^>]*?)>)/g, '')
.replace(/(<style[^>]*)(?:[^])*?\/style>/g, '')
.replace(/(<(\/?)html([^>]*?)>)/g, '')
.replace(/(<app-root[^>]*)(?:[^])*?\/app-root>/ig, '')
.replace(/(<\?[\s\S]*?\?>)|(<!DOCTYPE\s+\w+\s+\[[\s\S]*?\]>)|(<!\w[\s\S]*?>)/g, '')
.replace(/(href\s*=\s*(?:"))/ig, 'href="/#')
.replace(/(href\s*=\s*(?:'))/ig, "href='/#");
this.response = restr;
})
.catch(error => this.status = error);
}
ページが表示されます。しかし、スクリプトは機能しません。それらはdivタグに存在しますが、実行されません。私が試した
はeval()
でそれを行うが、innerHTML
がロードされたスクリプトタグを実行しません。なぜこれが悪い
let scripts: string[] = restr.match(/\<scr[\s\S]*?ipt>/g);
this.srcfield.nativeElement.innerHTML = '';
scripts.forEach((value, index) => {
eval.call(null, (this.srcfield.nativeElement.innerHTML = value));
});
SyntaxError: Unexpected token <
で終了しますか?どのように私はそれを修正することができます?テンプレートで