javascript - AngularJS - State name passed into controller doesn't work -
i'm new angular , can't figure out why isn't working.
when call {{$state.current.name}}
in view works fine, try pass controller, state gets "lost".
here setup:
module:
angular.module('app', ['ui.router']) .run(function ($rootscope, $state, $stateparams) { $rootscope.$state = $state; $rootscope.$stateparams = $stateparams; }) //followed boilerplate ui router setup
view:
<home-nav-bar></home-nav-bar>
directive:
angular.module('app') .directive('homenavbar', function () { return { restrict: 'e', templateurl: 'homenavbarview.html', controller: 'navctrl' }; }) })();
controller:
angular.module('app') .controller('navctrl', function ($state, $scope) { alert($state.current.name); //returns empty }); })();
at point clueless why can state name in view not in controller..
well, ui-router
docs can access current state's config object using current
property of $state
, there absolutely no need attach $rootscope
. have tested along lines of (simplified bit readability):
angular.module('myapp') .controller('somectrl', function ($scope, $state) { console.log($state.current); });
the result in chrome console is:
object {url: "/some", templateurl: "app/some.html", controller: "somectrl", name: "some"}
so can see information should available along name.
Comments
Post a Comment