Remember once Angularjs Route $stateChangeStart does not work (details determine success or failure)

Source: Internet
Author: User

var app= angular
. Module (' app ', [
' Nganimate ',
' Ngcookies ',
' Ngresource ',
' Ngsanitize ',
' Ngtouch ',
' Ui.router ',
' Ui.bootstrap ',
' Datatablesdirectives ',
' Ngtable ',
' Ncy-angular-breadcrumb ',
' Ngfileupload ',
' Messagecentermodule ',
' Angular-loading-bar ',
' Toggle-switch '
])
. Run (
[' $rootScope ', ' $state ', ' $stateParams ', ' appsession ',
function ($rootScope, $state, $stateParams, appsession) {
$rootScope. $state = $state;
$rootScope. $stateParams = $stateParams;
$rootScope. $on (' $stateChangeStart ', function (event, tostate, Toparams, FromState, Fromparams) {
$state. previous = FromState;
$state. previousparams = Fromparams;
if (ToState.data.requiredLogin &&!) Appsession.getlogin () &&!toparams.tokenandname) {
$state. Go (' login ', {}, {
Reload:true
});
Event.preventdefault ();
}
});
}
]
). config ([' Cfploadingbarprovider ', function (Cfploadingbarprovider) {
Cfploadingbarprovider.includespinner = true;
}])
. config (
[' $stateProvider ', ' $urlRouterProvider ', ' $breadcrumbProvider ', ' $httpProvider ',
function ($stateProvider, $urlRouterProvider, $breadcrumbProvider, $httpProvider) {
$httpProvider. Interceptors.push (' Tokeninterceptor ');


$urlRouterProvider. Otherwise ('/');
$stateProvider
. State (' login ', {
URL: '/',
Templateurl: ' views/login/login.html ',
Controller: ' Loginctrl ',
Data: {
Requiredlogin:false
}
})
. State (' main ', {
URL: '/main ',
Abstract:true,
Templateurl: ' main.html ',
Data: {
Requiredlogin:true
},
Ncybreadcrumb: {
Label: ' Home '
}
})
. State (' Main.mywork ', {
URL: '/mywork ',
Templateurl: ' views/mywork/myworklist.html ',
Controller: ' Myworkcontroller ',
Ncybreadcrumb: {
Label: ' My Workbench ',
Parent: ' Main '
},
Data: {
Requiredlogin:true
}
}).......
Status Change Events
 $scope  . $on ( ' $ Statechangestart ' function ) { //If you need to block the completion of the event            
event. preventdefault();   

});

   < Span class= "token punctuation" > parameters:          
event:该事件的基本信息
toState:我们可以得到当前路由的信息,比如路由名称,url,视图的控制器,模板路径等等
toParams:We can get the parameters of the current route
fromState:We can get information on the last route, such as the route name, URL, view controller, template path, etc.
fromParams:We can get the parameters of the previous route

The events that can be triggered include:

Statechangestart

Triggered when the state change is started

$stateChangeSuccess

Triggered when the status change is successful

$stateChangeError

When a state change encounters an error, the error is usually that the target cannot be loaded, the pre-loaded data cannot be loaded, and so on.

According to the above: the user is not logged in the case, jump to the routing main.mywork, should be redirected to the login page. But found jumps.
After several twists and turns, found himself too careless. Not introduced

The ' ui.router.state.events ' module, index.html page also did not introduce a response to the stateevents.js.

Once Angularjs routing $stateChangeStart not working (details determine success or failure)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.