dilagare sul angolare e ui-router
E alle prese con il reindirizzamento ad uno stato diverso se una precondizione non è soddisfatta:
Ho provato ad utilizzare un intercettore: (How do I preform a redirect in an angular interceptor).
Ma qualcuno ha detto che gestire $ stateChangeState sarebbe più appropriato. Ma io sono ancora in esecuzione in un ciclo infinito:
/**
* Check here for preconditions of state transitions
*/
$rootScope.$on('$stateChangeStart', function(event, toState) {
// which states in accounts to be selected
var accountRequiredStates = ['user.list', 'user.new'];
if(_.contains(accountRequiredStates, toState.name)){
event.preventDefault();
ApiAccount.customGET('get_current').then(function(resp){
// if I have a selected account, go about your business
if(resp.hasOwnProperty('id')){
$state.go(toState.name);
} else { // prompt user to select account
$state.go('user.select_account');
}
})
}
});
Qualcuno può suggerire un modello migliore (quello che funziona)
Grazie!
Nota: problema simile approccio diverso qui: How do I preform a redirect in an angular interceptor
si dovrebbe solo prevenire l'evento se si desidera reindirizzare. Immagino che '$ state.go' debba essere inserito in una funzione' $ timeout'. – zeroflagL
grazie - potresti condividere un codice snipit? – Jonathan
Penso che la tua domanda sia duplciata con http://stackoverflow.com/questions/21631896/why-does-this-angular-ui-router-code-cause-an-infinite-loop-in-digest – linkary