- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
注意:这是一个示例代码,window.has_redirected
只是为了让问题更容易。
此代码不会重定向到 dashboard
。
我在网上看到它不起作用的原因是 $state
还没有准备好。但是我在网上看到的教程与此非常相似。
我该如何解决?
(function(){
"use strict";
angular.module('app.routes').config( function($stateProvider, $urlRouterProvider ) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('login',{
url: '/',
...
})
.state('dashboard', {
url: '/dashboard',
...
});
} ).run( function( $rootScope, Users, $state ) {
$rootScope.$on( "$stateChangeStart", function(event, toState, toParams
, fromState, fromParams){
if ( !window.has_redirected ){
window.has_redirected = true;
window.console.log('going to dashboard');
$state.go('dashboard');
}
});
} );
})();
最佳答案
你快到了……我们真正需要的是停止执行流程:
event.preventDefault();
这将停止当前状态更改,并将重定向 ($state.go()
):
$rootScope.$on( "$stateChangeStart", function(event, toState, toParams
, fromState, fromParams){
// I would always check if we are not already on the way
// to the "redirection" target.
var isGoingToDashboard = toState.name === "dashboard";
if(isGoingToDashboard)
{
return;
}
// this remains
if ( !window.has_redirected ){
window.has_redirected = true;
console.log('going to dashboard');
// HERE is the essential breaker of the execution
event.preventDefault();
$state.go('dashboard');
}
});
关于javascript - $state.go 在 app.run 中的 $stateChangeStart 内不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30247273/
我正在使用 $stateChangeStart 进行登录。 在我的网站中,我有一些页面,用户在未登录时可以访问这些页面。 我编写此代码是为了注销并检查某些页面上的访问权限。 但是当用户转到有关页面时我
$rootScope.$on('$stateChangeStart', function(event, toState , toParams
请, 我在我的项目中使用 ui-router。 $stateChangeStart当用户在状态之间导航时,事件工作正常。 但是如果我刷新浏览器,它就不起作用。 这是我的源代码: (function()
我的 app.run() 中有以下代码: window.audienceConfig = InstitutionConfiguration.checkConfiguration(INSTITUTION
有没有办法从父状态获取参数。 我的状态是这样的。 $stateProvider .state('home', { url: '/home/:param1
我正在尝试构建我想在根状态解析上初始化的 Angular 色权限系统: $stateProvider .state('common', { resolve:{ user: f
我正在尝试使用 AngularUI 路由器进行一些身份验证。 $urlRouter.sync()看起来正是我需要的。但是,这仅在我拦截 $locationChangeSuccess 时可用.但是当我这
加强 Angular 和ui-router 如果不满足先决条件,则很难重定向到不同的状态: 我尝试使用拦截器:( How do I preform a redirect in an angular i
当应用程序更改路由时,我的 AngularJs 运行 block 不会执行处理程序函数,至少我是这么认为的。我尝试放置一个简单的控制台日志来测试,但它没有记录任何内容。最终目标是成为一个路由拦截器,检
这是我的 app.js var $stateProviderRef = null; var $urlRouterProviderRef = null; var acadb = angular.modu
我试图在加载 Home 子状态之前解析抽象父状态中的一些服务器端数据。我想确保在州的 data.rule 函数中我有用户的全名以供检查。但是,使用 console.log 语句,我可以看到 userF
我正在尝试为用户实现基于路由的安全性,但 stateChangeStart 没有触发,这是我的代码: app.run(['$rootScope', '$location', function($roo
我想知道这三个有什么区别,它们对应的$locationChangeSuccess , $routeChangeSuccess , 和 $stateChangeSuccess . 最佳答案 $locat
我想知道是否有替代方案 $rootScope.$on("$stateChangeStart", function() {}); 因为我想在许多执行不同操作的页面上获取该事件。如果我使用上面的方法,那么
我使用 this创建我的网站,当我创建一个带有 $rootScope.on('$stateChangeStart') 的运行方法时,它不起作用。这是代码: .run('Url',function($r
我使用 this创建我的网站,当我创建一个带有 $rootScope.on('$stateChangeStart') 的运行方法时,它不起作用。这是代码: .run('Url',function($r
我在我的项目中使用 AngularJs,并在其中使用 ui-router。 当用户单击链接时,我需要先轻轻淡出当前 View ,然后再将其导航到另一个 View 。 我已经为此编写了指令,这是它的链接
我尝试进行用户登录身份验证,我创建了一个名为 AuthService 的服务,方法是 isAuthenticated,我在其中调用 API 进行交叉检查用户是否登录(稍后我将在其中使用用户 Angul
我有一个 angular 应用程序,它使用授权逻辑和 ui-router 来禁止未经授权的用户访问某些状态/ View 。我遵循监听 stateChange 事件的标准方法,该事件触发我的授权逻辑。这
我的目标是将 token 作为授权 header 发送到我的应用程序中的每个服务调用。 这是我的代码 'use strict'; var properties = {}; angular.module
我是一名优秀的程序员,十分优秀!