Angular 当 url 没有参数时重定向

Angular Redirect when url has no parameters

当用户丢失密码时,会向他的帐户发送一封带有 URL 的电子邮件。我希望控制重置密码的页面仅在 url 具有参数时加载。我正在使用 Angular。

例如:

如果用户转到 http://example.com/reset_password/A232Ddade
正常加载 reset_password/reset_password.html 页面

但是

如果用户转到 http://example.com/reset_password/ 重定向到 http://example.com/index.html

另外要考虑的是,在渲染页面之前,我会使用参数找到将更改密码的用户。

为了做到这一点,我必须使用一些初始化函数,读取参数,如果存在则使用它,或者如果不存在则重定向到索引页面?喜欢

//At the top of the controller
var init = function () {
   //check if there is query in url
   //and redirect if not 
};
 //and fire it after definition
init();

或者我可以使用 ui-router 模块吗?

谢谢。

看起来像 $routeProvider

的标准用法
app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
            when('/reset_password/:param', {templateUrl: 'pageToReset.html',   controller: ResetLogicCtrl}).
            when('/reset_password/', {templateUrl: 'index.html',   controller: IndexCtrl}).
            otherwise({redirectTo: '/'});
}]);

您可以在您的路线上使用 Resolve 来做到这一点。获取参数并获取用户,如果没有用户,或者如果参数是假值,则重定向到您想要的任何位置。这样您的控制器就可以获取所需的数据或进行重定向。