UI的路由器$状态$当前包装器arbitary状态 [英] UI-Router $state.$current wrapper for arbitary state

查看:132
本文介绍了UI的路由器$状态$当前包装器arbitary状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是用例。给定一个stateConfig对象,我可以访问 state.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.

幸运的是, $状态。$电流提供了一个扩展状态对象,这让我反复地穿越一个国家的父母和建立配套完整的URL。 Unforunately, $状态。$电流明明只有包装了当前的状态,但是这将是美好的,如果我能以同样的方式换一个任意状态。有任何想法吗?

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?

推荐答案

您可以通过使用公开的内部状态实施 .decorator 勾上 $ stateProvider 。您可以装点国家建设者的任何财产;我选择了父随意。

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); 
  });
});


现在您可以通过访问内部状态对象。$$状态(),e.gg

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

这篇关于UI的路由器$状态$当前包装器arbitary状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆