rekna
rekna

Reputation: 5333

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?

Upvotes: 9

Views: 8648

Answers (1)

moribvndvs
moribvndvs

Reputation: 42497

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.

Upvotes: 2

Related Questions