1

I'm trying to get the $state of a URL.

Basically I want to do the reverse of :

$state.href('user.home.view', {}, {absolute: true});

Instead of get The Abs URL I want to get The state's name;

I want to pass a Abs URL to a function and then get a valid $state in the angular's project.

2
  • 1
    when in doubt ... console.log($state) and inspect what's there Commented Feb 26, 2016 at 21:15
  • check the updated answer if that may help you somewhat. Not exactly what you wanted, but may suffice your need. Commented Feb 26, 2016 at 22:09

1 Answer 1

2

There is a data field named $uiView attached to the ui-view element, it contains the view name and the associated state. You can get the state like this:

elem.closest('[ui-view]').data('$uiView').state

or even

elem.inheritedData('$uiView').state

So, in your controller:

 .controller('State1Ctrl', function ($state) {
      console.log(elem.closest('[ui-view]').data('$uiView').state); // state1
      console.log($state.current.name)  ;//will give the state name as well.  
});  

UPDATE:

Your issue: https://github.com/angular-ui/ui-router/issues/1651

WORKAROUND:
ANGULAR-UI-ROUTER: Resolve state from URL

You can expose the internal state implementation by using the .decorator hook on $stateProvider. You can decorate any property of the state builder; somebody 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.g.

var publicState = $state.get("foo");
    var privateInternalState = publicState.$$state();
    //Second, loop over each state in $state.get() and test them against your URL fragment.

    angular.forEach($state.get(), function(state) { 
      var privatePortion = state.$$state();
      var match = privatePortion.url.exec(url, queryParams);
      if (match) console.log("Matched state: " + state.name + " and parameters: " + match);
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Just updated the question... I want to pass an URL an get a valid $state in the application... is that possible?
I don't know about that, since the documentation mentions none of what you are expecting for now. We are talking about absURL which may or may not resolve to a state. There may be some hack around though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.