问题描述
我的UI一个路由器设置是这样的:
My ui router config is like this:
$stateProvider
.state("list",{url:"/list",templateUrl:"list.html",controller:"ctrl as vm"})
.state("list.select",
url:'/select',
templateUrl:'select.html',
onEnter:function( ) { ... }
});
该list.select国家使用相同的控制器列表中的状态。我怎么能叫从的OnEnter功能的控制器上的方法是什么?请注意,我用Ctrl键VM语法!可我也在这里访问$ stateParams?
The list.select state uses the same controller as the list state. How can I call a method on the controller from the onEnter function? Note that I'm using the "ctrl as vm" syntax! Can I also access $stateParams here?
推荐答案
您当然可以访问 $ stateParams
在的OnEnter
,以及任何其他的服务。然而,没有办法来注入电流或父(或任何其他)控制器实例
You can certainly access $stateParams
in onEnter
, as well as any other service. However, there's no way to inject the current or parent (or any other) controller instance.
所以,虽然你不能调用控制器上的方法,这样,你可以使用的OnEnter
或决心
为preprocess东西,也许用一个标志 list.select
来检查并调用该方法。
So, while you can't invoke a method on the controller this way, you can use onEnter
or resolve
to preprocess something and perhaps use a flag for list.select
to check and call that method.
这也可能更合理使用服务来协调此功能,但我不知道你的做法的目的,所以我需要知道更多。
It also may make more sense to use a service to coordinate this functionality, but I don't know the purposes of your approach so I'd need to know more.
这篇关于角UI路由器:调用方法从控制器的OnEnter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!