我对AngularJS配置功能有以下含糊之处。 似乎配置功能是同步的。我想要求 一个后端服务并根据其响应构建路由。 当我删除请求和$ q一切正常。
someApp.config(['$routeProvider', '$httpProvider', '$compileProvider', 'settings',
function ($routeProvider, $httpProvider, $compileProvider, settings) {
var $http = angular.injector(['ng']).get('$http');
var $q = angular.injector(['ng']).get('$q');
var isUserLogged = $q.defer();
$http.get(settings.apiUri + settings.loginStatus).success(function (data) {
if (data.status == "NOT_LOGGED") {
isUserLogged.reject();
} else {
isUserLogged.resolve();
}
});
isUserLogged.promise.catch(function () {
$routeProvider.
when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeController'
}).
otherwise({
redirectTo: '/home'
});
});
isUserLogged.promise.then(function () {
$routeProvider.
when('/accountsettings', {
templateUrl: 'partials/accountsettings.html',
controller: 'AccountSettingsController'
}).
otherwise({
redirectTo: '/accountsettings'
});
});
}]);
答案 0 :(得分:0)
显然我必须自己回答。首先,我请求确定用户是否已登录。然后我触发自定义事件监听器,其调用应用程序的.config方法,并根据用户是否记录进行路由。以下是我得到的决定代码(带缩写)。 Here链接到我的博客,在那里我写了更多关于这个主题的内容。
angular.element(document).one("userStatusIsCheked",
function(event, userIsLogged) {
appName.config(['$routeProvider',
'$httpProvider', '$compileProvider',
function ($routeProvider,
$httpProvider, $compileProvider) {
if (userIsLogged == true) {
$routeProvider.
when('/resetpassword/:hashValue', {
templateUrl: 'partials/home.html',
controller: 'ResetPasswordController'
}).
when('/accountsettings', {
templateUrl: 'partials/accountsettings.html',
controller: 'AccountSettingsController'
}).
when('/changepassword',
{
templateUrl:
'partials/accountsettingschangepassword.html',
controller: 'ChangePasswordController'
}).
when('/edit', {
templateUrl: 'partials/accountsettingsedit.html',
controller: 'EditAccountController'
}).
when('/apikeymanagement', {
templateUrl: 'partials/apikeymanagement.html',
controller: 'APIKeyManagementController'
}).
when('/dashboard', {
templateUrl: 'partials/dashboard.html',
controller: 'DashboardController'
}).
when('/pipelines', {
templateUrl: 'partials/pipelines.html',
controller: 'PipeLinesController'
}).
when('/preferences', {
templateUrl: 'partials/preferences.html',
controller: 'PreferencesController'
}).
when('/reports', {
templateUrl: 'partials/reports.html',
controller: 'ReportsController'
}).
when('/logout', {
templateUrl: 'partials/logout.html',
controller: 'LogoutController',
resolve: {
logout: function (logout, $q) {
var deferred = $q.defer();
logout()['finally'](
function() {
deferred.resolve();
}
);
return deferred.promise;
}
}
}).
when('/privacy', {
templateUrl: 'partials/privacy.html',
controller: 'PrivacyController'
}).
when('/tos', {
templateUrl: 'partials/tos.html',
controller: 'TosController'
}).
when('/currentStatus', {
templateUrl: 'partials/currentStatus.html',
controller: 'CurrentStatusController'
}).
otherwise({
redirectTo: '/dashboard'
});
} else {
/**
* If user is not logged default action is
* home.
*/
$routeProvider.
when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeController'
}).
when('/resetpassword/:hashValue', {
templateUrl: 'partials/home.html',
controller: 'ResetPasswordController'
}).
when('/privacy', {
templateUrl: 'partials/privacy.html',
controller: 'PrivacyController'
}).
when('/tos', {
templateUrl: 'partials/tos.html',
controller: 'TosController'
}).
when('/currentStatus', {
templateUrl: 'partials/currentStatus.html',
controller: 'CurrentStatusController'
}).
otherwise({
redirectTo: '/home'
});
}
}]);
angular.bootstrap(document, ['appName']);
});
angular.element(document).ready(function() {
var injector = angular.injector(['appName']);
var settings = injector.get('settings');
var $http = injector.get('$http');
var functionFactory = function (userStatus) {
return function () {
this.lUserStatus = userStatus;
return this.lUserStatus;
}
};
/**
* So here we will check if user is logged or
* not and after this construct conditional routing
* into config method (above).
*/
$http.get(settings.apiUri + settings.loginStatus).
success(function (data) {
if(angular.isDefined(data.status) &&
data.status == "NOT_AUTHENTICATED") {
appName.service('userIsLogged', functionFactory(false));
angular.element(document).trigger("userStatusIsCheked", [false]);
} else if(angular.isDefined(data.status) && data.
status == "AUTHENTICATED") {
appName.service('userIsLogged',
functionFactory(true));
angular.element(document).trigger("userStatusIsCheked", [true]);
}
});
});