Javascript 角度:在执行$location.path()时传递数据
我现在有两种观点Javascript 角度:在执行$location.path()时传递数据,javascript,angularjs,Javascript,Angularjs,我现在有两种观点 登录 主要 现在我登录并将路径更改为/main,效果很好。当我未登录并尝试访问/main时,我的web服务返回“拒绝匿名用户访问”,然后我将它们转发到/这是我的登录视图。如何传递某个信息,以便我的LoginController知道它们是从/main转发的,以提醒它们先登录 LoginController.js VforumJS.controller('LoginController', function($scope, $location, $routeParams, Lo
- 登录
- 主要
/main
,效果很好。当我未登录并尝试访问/main
时,我的web服务返回“拒绝匿名用户访问”
,然后我将它们转发到/
这是我的登录视图。如何传递某个信息,以便我的LoginController
知道它们是从/main
转发的,以提醒它们先登录
LoginController.js
VforumJS.controller('LoginController', function($scope, $location, $routeParams, LoginModel)
{
$scope.email = "";
$scope.password = "";
$scope.fetching = false;
$scope.error = null;
$scope.login = function()
{
$scope.error = null;
$scope.fetching = true;
LoginModel.login($scope.email, $scope.password);
}
$scope.$on('LoginComplete', function(event, args)
{
log('login complete: ' + args.result);
$scope.fetching = false;
if (args.result == "success")
{
$location.path('/main');
}
else
{
$scope.error = args.result;
}
});
});
VforumJS.controller('MainController', function($scope, $location, $routeParams, MainModel)
{
$scope.currentTitle = '-1';
$scope.presentationData = MainModel.getPresentations();
$scope.$on('PresentationsLoaded', function(event, args)
{
log(args.result);
if (args.result != "Access denied for user anonymous")
{
//-- Parse preso data
$scope.presentationData = args.result;
}
else
{
//-- Need to login first, route them back to login screen
$location.path("/");
}
});
});
MainController.js
VforumJS.controller('LoginController', function($scope, $location, $routeParams, LoginModel)
{
$scope.email = "";
$scope.password = "";
$scope.fetching = false;
$scope.error = null;
$scope.login = function()
{
$scope.error = null;
$scope.fetching = true;
LoginModel.login($scope.email, $scope.password);
}
$scope.$on('LoginComplete', function(event, args)
{
log('login complete: ' + args.result);
$scope.fetching = false;
if (args.result == "success")
{
$location.path('/main');
}
else
{
$scope.error = args.result;
}
});
});
VforumJS.controller('MainController', function($scope, $location, $routeParams, MainModel)
{
$scope.currentTitle = '-1';
$scope.presentationData = MainModel.getPresentations();
$scope.$on('PresentationsLoaded', function(event, args)
{
log(args.result);
if (args.result != "Access denied for user anonymous")
{
//-- Parse preso data
$scope.presentationData = args.result;
}
else
{
//-- Need to login first, route them back to login screen
$location.path("/");
}
});
});
您可以在
MainController
中使用$location.search()
将查询字符串传递给LoginController
在您的内部main控制器
:
if (args.result != "Access denied for user anonymous")
{
//-- Parse preso data
$scope.presentationData = args.result;
}
else
{
//-- Need to login first, route them back to login screen
$location.search({ redirectFrom: $location.path() });
$location.path("/");
}
然后在您的登录控制器中
,为简洁起见缩短:
VforumJS.controller('LoginController', function($scope, $location, $routeParams, LoginModel)
{
var queryString = $location.search();
$scope.$on('LoginComplete', function(event, args)
{
log('login complete: ' + args.result);
$scope.fetching = false;
if (args.result == "success")
{
if (queryString && queryString.redirectFrom) {
$location.path(queryString.redirectFrom);
} else {
$location.path('/somedefaultlocation');
}
}
else
{
$scope.error = args.result;
}
});
});
或者,您可以使用共享服务,甚至可以使用LoginModel从MainController设置一个参数,以指示重定向来自它
更新
更好的是,使用$httpProvider.interceptors
注册响应侦听器,然后使用上述相同的$location.search()
技术在身份验证失败时重定向到登录屏幕。此方法非常理想,因为您的控制器没有身份验证逻辑。广播$locationChangeStart
和$locationChangeSuccess
事件,每个事件的第三个参数是oldUrl
一种解决方案是使用订阅$locationChangeStart
的服务来保存当前和旧URL
当您点击
/
时,您的登录控制器可以检查您的服务,以查看旧URL是否为/main
,然后相应地采取行动。非常好,使用$location.search()对我有效,谢谢。