AngularJS UI Router - $state.current.name is empty when using templateUrl -
i using ui router angular , trying name of current state.
after 3 days working on it, found out empty when use templateurl. after changing template: 'test', state name magically appears...
is possible make work? here demo
code on above link
a $watch needed here, because it's asynchronous can't rely on timing:
myapp.controller('navctrl', ['$state', '$scope','$timeout', function($state, $scope, $timeout){ $scope.currstate = $state; console.log("this 1 have objects: "); console.log('by reference:', $scope.currstate); console.log('by value:', json.parse(json.stringify($scope.currstate))); // console.log("but when want access name empty: "); // $timeout(function() { // console.log($state.current.name); // }); // use instead: $scope.$watch('currstate.current.name', function(newvalue, oldvalue) { console.log(newvalue); }); }]);
the console.log('by reference:', $scope.currstate);
work because uses reference object. time see in console object changed.
compare output of console.log('by value:', json.parse(json.stringify($scope.currstate)));
above, freezes output @ point of execution. you'll find empty $state.current.name
.
also see: how can change default behavior of console.log? (*error console in safari, no add-on*)
Comments
Post a Comment