2014-11-09 121 views
1

这里是用例。给定一个stateConfig对象,我可以访问state.url,但这只返回该配置对象,不包括国家的父母的网址URL中指定的URL。我需要构建完整的URL以传递到$ urlMatcherFactory.compile,以测试匹配。

幸运的是,$state.$current提供了一个扩展状态对象,这让我反复地穿越一个国家的父母和建立配套的完整URL。不幸的是,$state.$current显然只包含当前状态,但如果我可以用相同的方式包装任意状态,那将是非常好的。有任何想法吗?

回答

2

您可以通过使用.decorator钩上$stateProvider暴露内部状态的实现。你可以装饰国家建设者的任何财产;我任意选择了“父母”。


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()内部状态的对象,e.gg

var publicState = $state.get("foo"); 
var privateInternalState = publicState.$$state(); 
+0

克里斯你是一个芭蕾舞者。非常感谢。 – Derek 2014-11-11 04:50:39

0

明白了。 有点测井后,我意识到,主叫state.$$state()将返回卷绕状态配置对象。

+0

'状态。$$状态()'是一个'UI的路由器演员'功能。如果你想访问没有'ui-router-extras'的内部状态,你可以使用'$ stateProvider.decorator()'函数注册一个装饰器,然后捕获内部状态。这就是ui-router-extras能够暴露'state。$$ state()'能力的方式。 – 2014-11-09 21:31:59

+0

对不起,我以前的评论措辞不佳。我了解装饰者如何在文档中工作,但我不知道如何揭示它们。例如,对于父装饰器,我怎样才能将它暴露在状态对象上? – Derek 2014-11-10 06:03:58

+0

发布为答案。 – 2014-11-10 16:38:09