AngularJS: Reload ng-include after user authentication (or a better way to solve issue) AngularJS: Reload ng-include after user authentication (or a better way to solve issue) angularjs angularjs

AngularJS: Reload ng-include after user authentication (or a better way to solve issue)


You can start with raising an event when user login is success on the rootscope using broadcast method

$rootScope.$broadcast('userLoggedIn',{user:user});

On the appController you can subscribe to the even

$scope.$on("userLoggedIn",function(event,args) {     $scope.menuUrl=null;     $scope.menuUrl="partials/nav.html";});

this also implies that change your menuUrl to a property in you controller and html binding.

Alos, if you can define multiple server views for logged in and anonymous user, then you can just flip the view on user login.


@Chandermani 's answer is almost close, in fact, you can just add some random params to force refresh, just like this:

app.controller('appController', function($scope){   var getMenu = function(){     var random = Math.random();     return "partials/nav.html?r=" + random;   }   $scope.menuUrl = getMenu();   $scope.$on('userLoggedIn', function(){     $scope.menuUrl = getMenu();   })})