0

I am migrating from UI-Router v0.4.x to v1.0 and I'm having an issue. Consider the following piece of code.

myApp
    .component('myComponent', {
        bindings: {
            resolveBinding: '<'
        },
        controller: class myComponentController {
            constructor() {
                // ...
            }
        },
        templateUrl: someTemplateUrl
    })
    .config(($stateProvider) => {
        $stateProvider
            .state('some-state', {
                url: '/some-state',
                component: 'myComponent',
                resolve: {
                    resolveBinding: function(DependencyResolver) {
                        let targetStateName = this.self.name
                        return DependencyResolver.doSomeThingsFor(targetStateName)
                    }
                }
            })
    })

In the new versionlet targetStateName = this.self.name will fail because this is now null, whereas before it contained information on the state it was transitioning to.

How can I get the state name in this block of code?

I was thinking about using a $transitions.onBefore hook to put the name on rootScope, and doing something like:

resolveBinding: function($rootScope, DependencyResolver) {
    let targetStateName = $rootScope.hackyStateName 
    return DependencyResolver.doSomeThingsFor(targetStateName)
}

But I feel this is ugly and I'm missing out on something easier and more elegant.

1 Answer 1

2

You can inject $transition$ into a resolve function:

resolveBinding: function($transition$) {
  console.log($transition$.to());
}

See the $transition$ documentation.

Does that help you?

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

1 Comment

Exactly what I was searching for. Thank you for pointing this out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.