我在一条路线中显示已清理的HTML,其中包含指向路由器其他部分的链接。但是,单击它们会导致网站重新加载。
如何从angular2范围外传递链接到路由器?
实施例
article.component.ts
@Component({
selector: "article",
template: `<div [innerHTML]="content"></div>`, //html is not parsed by ng because its outside the scope
providers: []
})
export class ArticleComponent{
public content;
constructor(private sanitizer: DomSanitizer){
this.content = this.sanitizer.bypassSecurityTrustHtml(`<a href="/article/some_id">example link</a>`);
}
}
在我的情况下,this.content是通过http请求下载的动态html,这就是它必须被清理的原因。
app.routing.ts
const appRoutes: Routes = [
{
path: "article/:id",
component: ArticleComponent
}
]
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);