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

前端 未结 2 360
清酒与你
清酒与你 2021-01-25 04:27

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 in

相关标签:
2条回答
  • 2021-01-25 05:05

    Got it. After a bit of logging, i realized that calling state.$$state() will return the wrapped state config object.

    0 讨论(0)
  • 2021-01-25 05:20

    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();
    
    0 讨论(0)
提交回复
热议问题