我试图在我的Ionic项目中使用角色来实现Auth。问题是我所关注的项目已经过时,因此项目结构存在太大差异。
这是我关注的项目:
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合并? 我需要制作另一个剧本吗?我想保留我的项目结构。
谢谢!
答案 0 :(得分:0)
您可以将这些功能复制到app.js.根据您的要求,您可以在三个空间中粘贴全局功能。 1)里面.run 2)在.config中 3)在这两者之外
不要忘记依赖