1

Here is the use case. Given a stateConfig object, I can access state.url, but this only returns the URL specified in that configuration object, not the URL that includes the URL's of a state's parents. I need to build the full URL to pass into $urlMatcherFactory.compile, to test for matches.

Fortunately, $state.$current provides an extended state object, which allows me to iteratively traverse a state's parents and build the full URL for matching. Unforunately, $state.$current obviously only wraps the current state, but it would be wonderful if I could wrap an arbitrary state in the same way. Any ideas?

Derek
  • 722
  • 1
  • 6
  • 16

2 Answers2

2

You can expose the internal state implementation by using the .decorator hook on $stateProvider. You can decorate any property of the state builder; I chose 'parent' arbitrarily.


app.config(function($stateProvider) { 
  $stateProvider.decorator('parent', function (internalStateObj, parentFn) {
     // This fn is called by StateBuilder each time a state is registered

     // The first arg is the internal state. Capture it and add an accessor to public state object.
     internalStateObj.self.$$state = function() { return internalStateObj; };

     // pass through to default .parent() function
     return parentFn(internalStateObj); 
  });
});

Now you can access the internal state object using .$$state(), e.gg

var publicState = $state.get("foo");
var privateInternalState = publicState.$$state();
Chris T
  • 8,186
  • 2
  • 29
  • 39
0

Got it. After a bit of logging, i realized that calling state.$$state() will return the wrapped state config object.

Derek
  • 722
  • 1
  • 6
  • 16
  • `state.$$state()` is a `ui-router-extras` feature. If you want to access the internal state without `ui-router-extras`, you can register a decorator with the `$stateProvider.decorator()` function and then capture the internal states. This is how ui-router-extras is able to expose the `state.$$state()` capability. – Chris T Nov 09 '14 at 21:31
  • Sorry my previous comment was poorly worded. I understand how the decorators work in the docs, but I don't get how to expose them. For instance, with the parent decorator, how do I actually expose it on the state object? – Derek Nov 10 '14 at 06:03