Cold cucumber Cold cucumber - 1 year ago 36
AngularJS Question

Angular - object $state with property current returning different value than $state.current

I am debugging my angular application and I wish to get the current $state.

When I

console.log($state)
and view the object here is what the parameter 'current' looks like

current:
Object controller:"projectsController"
controllerAs:"vm"
name:"app.project"
templateUrl:"app/controllers/project/template.html"
url:"/Project/{id}"


However, when I
console.log($state.current)
,

Here is how it looks like:

abstract:true
name:""
url:"^"
views:null


Why are they different? I fail to understand.

My code is simply

console.log($state.current)
console.log($state)

Answer Source

You need to wait until $state.current resolve. Try this: $timeout(function () { console.log($state.current, 'Should console now'); });

There is better way of handling state change event here:

$scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) { // toState === $state.current });

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download