我已经HTML通过http.get()方法加载页面,并且我将此页面的内容添加到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 );
}
Run Code Online (Sandbox Code Playgroud)
你怎么看,这个方法,把响应放在变量中,并用正则表达式解析字符串确定,然后我把它添加到div,就像这样
<div [innerHTML]="response | safe"></div>
Run Code Online (Sandbox Code Playgroud)
好,我的页面显示.但是,脚本不起作用.它们存在于div标签中,但不执行.
我曾尝试过这样做,eval()但结果很糟糕
let scripts: string[] = restr.match(/\<scr[\s\S]*?ipt>/g);
this.srcfield.nativeElement.innerHTML = '';
scripts.forEach((value, index) …Run Code Online (Sandbox Code Playgroud) 我使用angular2路由器,但直接路径出现问题。如果我写localhost:3000/container
页面将找不到。我知道如果我包括ROUTER_PROVIDERS,那将是可行的。但是我没有ROUTER_PROVIDERS和locationstrategy
看我的屏幕