2016-04-29 51 views
2

我有一个带有JS功能的TypeScript组件。该功能由Kendo UI调用,因此该功能在Javascript上下文中执行,这意味着我不能使用“this”(这将返回调用者Kendo组件)。我想在这个函数中重定向页面,但我宁愿使用router.navigate *函数而不是location.href替换。是否有可能在Javascript上下文中访问路由器实例?使用Javascript代码访问路由器

export class MyComponent implements AfterViewInit { 
    //template: `<div #grid></div>` 
    @ViewChild('grid') private grid: ElementRef; 

    ngAfterViewInit() { 
     this.configureGrid(); 
    } 

    private configureGrid() { 
     let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid"); 
     let kendoColumn: kendo.ui.GridColumn = {}; 
     kendoColumn.command = { text: "My Button", click: this.myButtonCommand, className: "btn" }; 
     kendoGrid.columns.add(kendoColumn); 
    } 

    myButtonCommand(e) { 
     //redirect here 
    } 
} 
+2

请出示一些代码。 –

+0

你可以使用'var that = this;'trick。 https://www.google.com/?gfe_rd=cr&ei=20AjV9HqFMaFzAW_grrgCg&gws_rd=ssl#q=javascript+this+that –

+1

您是否尝试过{{text:“我的按钮”,点击:this.myButtonCommand.bind(this), className:“btn”};'? –

回答

1

我认为这应该工作

private configureGrid() { 
    let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid"); 
    let kendoColumn: kendo.ui.GridColumn = {}; 
    kendoColumn.command = { text: "My Button", click: (e) => this.myButtonCommand(e), className: "btn" }; 
    kendoGrid.columns.add(kendoColumn); 
} 

,你应该能够只是调用this.router.navigate...()myButtonCommand()

+1

谢谢。我还想补充说,你的“点击:this.myButtonCommand.bind(this)”方法也可以。 – dstr