问题
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?
回答1:
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();
回答2:
Got it.
After a bit of logging, i realized that calling state.$$state()
will return the wrapped state config object.
来源:https://stackoverflow.com/questions/26824628/ui-router-state-current-wrapper-for-arbitary-state