Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ui router : calling method on controller from onEnter

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( ) { ... }
    });

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?

like image 886
rekna Avatar asked Dec 15 '13 21:12

rekna


1 Answers

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.

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.

like image 176
moribvndvs Avatar answered Sep 22 '22 04:09

moribvndvs