简体   繁体   English

Angular 1.5组件$ router绑定

[英]Angular 1.5 component $router binding

I'm trying to switch a 1.5x project over to components and use the new component-router. 我正在尝试将1.5x项目切换到组件并使用新的组件路由器。 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. 我在如何以编程方式访问$router遇到了一个问题,这些组件不在指令中。 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 . 基本上,我具有标题和侧边栏组件,它们需要能够执行$router.navigvate ,但是使用绑定: {$router: "<"}给了我一个未定义的$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> ). 当前在我的Index.html中,我在主体上定义了ng-app,然后在其中定义了主应用程序模块( <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. 因此,app.html具有<header>组件, <sidebar>组件,然后具有<ng-outlet>作为主要内容。 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. 使用UI-Router,我只在标题/侧边栏控制器中使用了$ state服务来查找活动状态或导航到新状态。

With component-router I can't figure out how to get the $router into the header/sidebar controllers. 使用component-router我无法弄清楚如何将$ router放入标题/侧边栏控制器中。

Any ideas? 有任何想法吗?

在控制器中使用$ rootRouter

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM