AngularJS $location not updated properly when using $routeProvider -


i have angular js application defaultcontroller controls header of app. have other controllers 1 each view. views loaded in <main>. load views using $routeprovider code:

myapp.config(['$routeprovider',     function($routeprovider) {         $routeprovider.             when('/login', {                 templateurl: 'templates/login.html',                 controller: 'logincontroller'             }).             when('/dashboard', {                 templateurl: 'templates/dashboard.html',                 controller: 'dashboardcontroller'             }).             ... 

i trying display logout button inside header when loaded view dashboard , hide if loaded view login view. in order have on defaultcontroller $location object , add , remove classes logout button ng-class.

there 1 problem: $location gives me correct path first time load page, after change view (changed $routeprovider) variable not updated anymore, when on /#/dashboard , $location.url still on /login. here controller code:

controllers.controller('defaultcontroller', ['$scope', 'ipcookie', '$location', function($scope, ipcookie, $location) {     $scope.url = $location.url();     ... 

i tried $window.location.hash same result.

any idea?

edit: after accepted answer ve added on defaultcontroller in order make work

$scope.$on("$locationchangesuccess", function() {         $scope.url = $location.url();           }); 

the location updated in service after default controller loaded.

you can either inject $location service scope , make decisions in template based on (then automatically watched , re-evaluated) or listen $locationchangesuccess event.

when injecting, can $scope.location = $location , use <a ng-hide="location.path() != '/something'">.

$location broadcasts $locationchangesuccess on root scope, should able listen on whichever scope have available: $scope.$on( "$locationchangesuccess", function() { /* */ } );


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -