Angular 1.5组件$ router绑定 [英] Angular 1.5 component $router binding

查看:108
本文介绍了Angular 1.5组件$ router绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将1.5倍的项目切换到组件并使用新的组件路由器.我在如何以编程方式访问不在指令内部的几个组件中的$router时遇到问题.基本上,我具有需要能够执行$router.navigvate的标题和侧边栏组件,但是使用绑定:{$router: "<"}给了我一个未定义的$router.

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.

当前在我的Index.html中,我在主体上定义了ng-app,然后在其中定义了主应用程序模块(<app></app>). 我的主要应用程序组件有一个模板,基本上带有我的母版页".

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".

因此,app.html具有一个<header>组件,一个<sidebar>组件,然后具有<ng-outlet>作为主要内容.使用UI-Router,我只是在标题/侧边栏控制器中使用了$ state服务来查找活动状态或导航到新状态.

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.

对于component-router,我无法弄清楚如何将$ router放入页眉/侧边栏控制器中.

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

有什么想法吗?

推荐答案

在控制器中使用$ rootRouter

use $rootRouter in the controller

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

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

这篇关于Angular 1.5组件$ router绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆