2016-10-15 58 views
0

我試圖在我的Ionic項目中實現與角色的身份驗證。問題在於我所遵循的項目比較陳舊,所以項目結構差異太大。將全局函數放入app.js

這是我以下項目:

http://plnkr.co/edit/Mvrte4?p=preview

本項目具有以下腳本:

globalFunctions.js

/** 
* Contains functions that are added to the root AngularJs scope. 
*/ 
angular.module('loginApp').run(function($rootScope, $state, Auth, AUTH_EVENTS) { 

//before each state change, check if the user is logged in 
//and authorized to move onto the next state 
$rootScope.$on('$stateChangeStart', function (event, next) { 
    var authorizedRoles = next.data.authorizedRoles; 
    if (!Auth.isAuthorized(authorizedRoles)) { 
     event.preventDefault(); 
     if (Auth.isAuthenticated()) { 
     // user is not allowed 
     $rootScope.$broadcast(AUTH_EVENTS.notAuthorized); 
     } else { 
     // user is not logged in 
     $rootScope.$broadcast(AUTH_EVENTS.notAuthenticated); 
     } 
    } 
    }); 

/* To show current active state on menu */ 
$rootScope.getClass = function(path) { 
    if ($state.current.name == path) { 
     return "active"; 
    } else { 
     return ""; 
    } 
} 

$rootScope.logout = function(){ 
    Auth.logout(); 
}; 

}); 

所以...在我的項目中,我沒有像上面的腳本那樣的文件,但是我有: app.js

angular.module('restaurant', [ 
'ionic', 
'ionic.service.core', 
'ionic.service.push', 

'ngCordova', 
'ionic-toast', 
'LocalStorageModule', 
'firebase', 

'config', 
'restaurant.restaurant-cart', 
'restaurant.restaurant-delivery', 
'restaurant.categories', 
'restaurant.products', 
'restaurant.news', 
'restaurant.map', 
'restaurant.home', 
'restaurant.cancha', 
'restaurant.ultimasoperaciones', 
'restaurant.push', 
'restaurant.menu', 
'restaurant.listanegra', 
'restaurant.login', 
'restaurant.agenda', 
'restaurant.contacto', 
'restaurant.infocancha', 
'restaurant.wordpress', 
'restaurant.drupal', 
'restaurant.favorites', 
'gMaps' 
]) 

.value('_', window._) 

.run(function($ionicPlatform) { 
$ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 

    if (window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 
}); 
}) 

.config(function($urlRouterProvider) { 
// if none of the above states are matched, use this as the fallback 
    $urlRouterProvider.otherwise('/app/login'); 
}); 

如何合併globalFunctions.js與我的腳本app.js? 我需要製作另一個腳本嗎?我想保留我的項目結構。

謝謝!

+1

創建'utilities'角服務,並把所有的'globalFunctions'功能在裏面。 –

+0

好的。謝謝!! –

+0

嗯,我認爲你需要把angular.module作爲這兩者之間的共同點,並且在你的視圖中包含兩個文件(index.html或其他)。這很容易,那麼你不需要手動合併。 – Jigar7521

回答

0

你可以將這些函數複製到app.js.有三個空間可以根據您的需求粘貼您的全局函數。 1).RUN 2)內的.config 3)境內關外的兩個

不要忘記依賴