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

前端 未结 2 900
醉话见心
醉话见心 2021-02-05 20:37

I am really just learning Angular and I am attempting to create an app that limits content access based on authentication. I have the authentication part working (also using the

相关标签:
2条回答
  • 2021-02-05 20:57

    @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();
       })
    })
    
    0 讨论(0)
  • 2021-02-05 21:05

    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.

    0 讨论(0)
提交回复
热议问题