2016-05-03 13 views
2

Gibt es eine Möglichkeit, eine Art Middleware in Ionic zu erstellen, so wie Laravel?Ionic Middleware wie Laravel

Also habe ich mehrere Controller, und in jedem Controller möchte ich prüfen, ob Token im lokalen Speicher existiert. Scheint irgendwie überflüssig, den Code wieder zu kopieren/einzufügen

+0

Welche ionische Version? v1 oder v2? –

+0

Es ist Version 1 – Norgul

Antwort

1

Leider gibt es keine Middleware wie Laravel, aber was Sie tun können, ist auf Zustandsänderungen zu hören.

(function(){ 
    'use strict'; 
    angular.module('Your_Module', ['ionic', 'wtv_more']) 
    .run(['$rootScope', '$ionicPlatform', '$state', function($rootScope, $ionicPlatform, $state) { 

    $ionicPlatform.ready(function() { 
     //your ionic stuff 
    }); 

    $rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState, fromParams) { 
     // we check if we are not on the index screen so we don't have a infinite loop of redirects 
     // and we also check if there's a token on the sessionStorage 
     if(!~toState.name.indexOf('app.home') && sessionStorage.token !== 'something'){ 
     e.preventDefault(); 
     $state.go('app.home'); //redirect user back to home 
     } 
    }); 
    }]); 
})(); 
Verwandte Themen