admin管理员组

文章数量:1417070

AngularJS(1) is used in my UI. I need change behavior for default page. If user is not authorized he should navigate to login page other case -> special page.

Configuring anonymous module

anonimModule.config(function ($routeProvider, anonimTemplateManager, anonimUrlManager) {
  $routeProvider
      .when('/', {
          templateUrl: anonimTemplateManager.getLoginPath(),
          controller: 'loginCtrl'
      })

I can set only one time this behavior (only for login for example) but the special page is not accessible if user authorized. I tried to get access to $rootScope from when (to solve my issue) - unsuccessfully.

Is it possible to implement dynamic behavior for routing? Could I have example or idea to implement it please?

AngularJS(1) is used in my UI. I need change behavior for default page. If user is not authorized he should navigate to login page other case -> special page.

Configuring anonymous module

anonimModule.config(function ($routeProvider, anonimTemplateManager, anonimUrlManager) {
  $routeProvider
      .when('/', {
          templateUrl: anonimTemplateManager.getLoginPath(),
          controller: 'loginCtrl'
      })

I can set only one time this behavior (only for login for example) but the special page is not accessible if user authorized. I tried to get access to $rootScope from when (to solve my issue) - unsuccessfully.

Is it possible to implement dynamic behavior for routing? Could I have example or idea to implement it please?

Share Improve this question asked Dec 23, 2016 at 15:35 SerhiiSerhii 7,61516 gold badges69 silver badges126 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Try to use otherwise.

Just chain it, like so:

.when('/foo', {
   templateUrl: 'foo.html',
   controller: fooController
)
.when('/bar', {
   templateUrl: 'bar.html',
   controller: barController
)
.otherwise(
    redirect: '/foo'
)

You may use state routing and then you may use statechangestart event for example:-

.state('user.dashboard', {
        templateUrl: 'pages/my/dashboard/deshboard.html',
        controller: 'myDashboardController'
        url: '/dashboard',
         access: {
            loginRequired: true
        }

and then in app.js you may use $stateChangeStart and some auth service that will check if user is authrize to access that state or not

 $rootScope.$on('$stateChangeStart',
            function (event, toState) {
                var authorised;
                if (toState.access !== undefined) {
                    authorised = authorizationServie.authorize(toState.name);
                   if (authorised ===  false) {
                        $state.go("loginState");
                        event.preventDefault();
                    }
                }
            });

You can also write $state.go('/url') to move to another page in your anonimTemplateManager.getLoginPath() method.

few other methods for same work

  • $location.url();
  • $state.go('/url')
  • $window.location.href = '/index.html';

not tried everything.

you save authorization data in local Storage when you login in application، you should check any time state change if user do not login in your application,your application must route in login page or display alert... for doing this:

var app=angular.module("myApp",["LocalStorageModule"]);
app.run(function($rootScope,$state,localStorageService){
     rootScope.$on("$stateChangeSuccess", function (event, toState, parameters, from) {
     if(localStorageService.get("authorizationData") !== 'logined')
        $state.go("loginPage");
    });
}

even you can save previous state with from variable in stateChangeSuccess event parametr and save this in rooScope for ing back to current state.

本文标签: javascriptAngular default page routingStack Overflow