我正在尝试将登录引入用户在应用程序中导航的方式.
如果该页面符合特定要求,我假装将用户重定向到他导航到登录页面之前的页面
防止来自$ stateChangeStart的事件停止状态改变如预期但当我运行$ state.go('into_somewhere')时我进入一个无限循环
我的角度版本是1.3.1,ui-router是最新版本
.factory('RouteHistory', function ($rootScope,$log, $state, Auth, $urlRouter, $timeout) {
// after the user enter a page
var currentState = '';
// when the user is trying to access a page that he has not permissions
// or that requires the user to be logged in
var pendingState = '';
var isMenuTogglerVisible = false;
var skipFromStateVal = true;
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
event.preventDefault();
if (toState.name == 'login' && fromState.name != 'login'){
$log.log('Ui-router: changing to …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AngularUI路由器进行一些身份验证.$urlRouter.sync()看起来正是我需要的.但是,这只有在我拦截时才可用$locationChangeSuccess.但是当我这样做时,它$state.current.name是空的,而我希望它是当前的状态.
到目前为止,这是我的代码:
$rootScope.$on('$locationChangeSuccess', function(event, next, nextParams) {
event.preventDefault();
if ($state.current.name === 'login') {
return userService.isAuthenticated().then(function(response) {
var authenticated;
authenticated = response.authenticated;
return alert(authenticated);
});
}
});
Run Code Online (Sandbox Code Playgroud)
关于我做错了什么的指示?
在客户视图中,我有一个创建客户按钮,该按钮应加载位于customers.create.html其中的部分视图customers.html.
如何将当前视图"customers.html"替换为另一个视图"customers.create.html"?
customers.html客户
<div>
<button ng-click="create()" class="btn btn-default" ui-sref="customers.create">Create</button>
</div>
<div style="height:500px;" ng-grid="myOptions"></div>
Run Code Online (Sandbox Code Playgroud)
app.js
$stateProvider
.state('customers', {
url: "/customers",
templateUrl: "../views/customers.html",
controller: ['$scope', '$stateParams', '$state',
function ( $scope, $stateParams, $state){
}]
})
.state('customers.create', {
url: "/create",
templateUrl: "../views/customers.create.html"
})
Run Code Online (Sandbox Code Playgroud)
在单击创建按钮时,路径将更改为/ customers/create,而不是html视图,它将保持不变.
我不能把它<div ui-view></div>放在创建按钮下,因为那时客户数据网格和创建按钮仍然可见.
也许我不应该使用分层客户.创建视图?
我想有两个主页,第一个是未登录的用户,第二个是登录用户.
这是我目前的设置:
.config(function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
$urlRouterProvider
.otherwise('/');
$locationProvider.html5Mode(true);
$httpProvider.interceptors.push('authInterceptor');
})
.factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location) {
return {
// Add authorization token to headers
request: function (config) {
config.headers = config.headers || {};
if ($cookieStore.get('token')) {
config.headers.Authorization = 'Bearer ' + $cookieStore.get('token');
}
return config;
},
// Intercept 401s and redirect you to login
responseError: function(response) {
if(response.status === 401) {
$location.path('/login');
// remove any stale tokens
$cookieStore.remove('token');
return $q.reject(response);
}
else {
return $q.reject(response);
} …Run Code Online (Sandbox Code Playgroud)