1

I would like to ask how to stop loading application. I need this for showing modal window with login form. My idea is: User go to my webpage, first start LoginController which check if user is authenticated. If not, it displays the form but must be stopped loading next controllers and components (loading data by user id). Here is my concept:

    <body ng-controller="LoginController">
       controllers, components...
    </body>

app.controller("LoginController", function LoginController(){
  this.$onInit = function(){
     if(!authenticated()){
         showLoginModal();
         stopLoadingApp(); // what will be implementation?
     }
  }
})

I tried this, but not called:

app.controller("LoginController", function LoginController($rootScope){
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
                event.preventDefault(); //not called
        });
});

Please help me how to solve this problem. Thanks

bluray
  • 1,875
  • 5
  • 36
  • 68
  • I don't think it is good approach to check authentication in a root controller. you can make use of `app.run` function which is a standard practice. – Muhammed Anees Jan 12 '18 at 20:13
  • Thank you, please can you show me small example how to do this? – bluray Jan 12 '18 at 20:14
  • `app.controller("LoginController", function(){` change like this – Kalaikumar Thangasamy Jan 12 '18 at 20:15
  • Here is a good reference https://stackoverflow.com/questions/20969835/angularjs-login-and-authentication-in-each-route-and-controller – Muhammed Anees Jan 12 '18 at 20:16
  • `app.controller("LoginController", function(){` change like this and write authenticate in `app.run(function($rootScope){})` – Kalaikumar Thangasamy Jan 12 '18 at 20:22
  • But I can not use this `$rootScope.$on('$routeChangeStart', function (event) {})` I dont use routing i my app. Can I you other event? – bluray Jan 12 '18 at 20:27
  • @bluray this is a somewhat recent change in ui-router they no longer use those events you want to use transition hooks (pretty easy just look it up in the docs) https://ui-router.github.io/guide/transitions – shaunhusain Jan 12 '18 at 21:04

0 Answers0