9

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I solved the problem by using resolve on the state config. Not trying to call method on controller, but resolving the data needed for the controller with the state config resolve parameters
@rekna can you please provide your example ? I'm running in the same case. Thx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.