UI-Router $state.$current 任意状态的包装器 [英] UI-Router $state.$current wrapper for arbitary state
问题描述
这是用例.给定一个 stateConfig 对象,我可以访问 state.url
,但这只会返回在该配置对象中指定的 URL,而不是包含状态父 URL 的 URL.我需要构建完整的 URL 以传递到 $urlMatcherFactory.compile,以测试匹配.
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.
幸运的是,$state.$current
提供了一个扩展的状态对象,它允许我迭代地遍历一个状态的父级并构建完整的 URL 进行匹配.不幸的是,$state.$current
显然只包装了当前状态,但如果我能以同样的方式包装任意状态就太好了.有任何想法吗?
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?
推荐答案
您可以通过使用 $stateProvider
上的 .decorator
钩子来公开内部状态实现.您可以装饰状态生成器的任何属性;我随意选择了父母".
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);
});
});
<小时>
现在您可以使用 .$$state()
访问内部状态对象,例如
Now you can access the internal state object using .$$state()
, e.gg
var publicState = $state.get("foo");
var privateInternalState = publicState.$$state();
这篇关于UI-Router $state.$current 任意状态的包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!