UI-Router $state.$current wrapper for arbitary state

痞子三分冷 提交于 2019-12-20 03:53:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!