Angular 1.5 component $router binding

不打扰是莪最后的温柔 提交于 2019-12-10 11:39:14

问题


I'm trying to switch a 1.5x project over to components and use the new component-router. I'm having an issue with how to programmatically access the $router in a couple of components that aren't sitting inside of a directive. Basically I have header and sidebar components which need to be able to do $router.navigvate, but using bindings: {$router: "<"} gives me an undefined $router.

I think I might just need to restructure the app a little bit, but I'm not sure how to go about it.

Currently in my Index.html I have ng-app defined on the body, and then the main app module inside of that (<app></app>). My main app component has a template with basically my "masterpage".

So app.html has a <header> component, <sidebar> component, and then <ng-outlet> for the main content. With UI-Router I just used the $state service in the header/sidebar controllers to find the active state or navigate to a new state.

With component-router I can't figure out how to get the $router into the header/sidebar controllers.

Any ideas?


回答1:


use $rootRouter in the controller

function controller($rootRouter){ $rootRouter.navigate(["someComponent"]) or $rootRouter.navigateByUrl("/url") }



来源:https://stackoverflow.com/questions/37219354/angular-1-5-component-router-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!