加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在没有首先导航到index.html的情况下直接进入

发布时间:2020-12-17 17:24:48 所属栏目:安全 来源:网络整理
导读:我有一个使用ui-router的AngularJS应用程序.一切正常,但我希望有一个用户注册确认屏幕,如下所示: http://localhost:2757/Auth/confirmation 在我目前的配置中,当我打开带有此链接的浏览器页面时,它不会查找index.html,也不会进入/ Auth / confirm状态.我理
我有一个使用ui-router的AngularJS应用程序.一切正常,但我希望有一个用户注册确认屏幕,如下所示:

http://localhost:2757/Auth/confirmation

在我目前的配置中,当我打开带有此链接的浏览器页面时,它不会查找index.html,也不会进入/ Auth / confirm状态.我理解为什么会这样,但我不知道如何解决这个问题.

有人可以给我一些建议,告诉我如何/如果我可以建立一个链接,让我直接进入/ Auth /确认.

我能想到的可能是这样的:

http://localhost:2757/index.html?load=AuthConfirm

然后以某种方式检查一下,在index.html加载后检测到要转换到的新状态.

这是我的AppConfig文件:

var auth = {
    name: 'auth',url: '/Auth',views: {
        'root': {
            templateUrl: 'app/auth/partials/home.html'
        }

    }
};

var authContent = {
    name: 'auth.content',url: '/:content',views: {
        'root': {
            templateUrl: 'app/exams/partials/home.html'
        },'content': {
            templateUrl: function (stateParams) {
                return 'app/auth/partials/' + stateParams.content + '.html'
            }
        }
    }
}

这是我尝试过的东西:

http://localhost:2757/index.html<<带我到我的主索引页面 http://localhost:2757/index.html/Auth<<返回页面未找到 http://localhost:2757/index.html/Auth/register<<返回页面未找到

解决方法

我想说我们需要的是 – 让index.html成为一个SPA(单页面应用程序).然后我们将从UI-Router中内置的功能中获益:

.when() for redirection

Parameters:

what String | RegExp | UrlMatcher The incoming path that you want to redirect.
handler String | Function The path you want to redirect your user to.

handler as String

If handler is a string,it is treated as a redirect,and is interpolated according to the syntax of match (i.e. like String.replace() for RegExp,or like a UrlMatcher pattern otherwise).

app.config(function($urlRouterProvider){
    // when there is an empty route,redirect to /index   
    $urlRouterProvider.when('','/index');

    // You can also use regex for the match parameter
    $urlRouterProvider.when(/aspx/i,'/index');
})

.otherwise() for invalid routes

Parameters:

path String | Function The url path you want to redirect to or a function rule that returns the url path. The function version is passed two params: $injector and $location.

app.config(function($urlRouterProvider){
    // if the path doesn't match any of the urls you configured
    // otherwise will take care of routing the user to the specified url
    $urlRouterProvider.otherwise('/index');

    // Example of using function rule as param
    $urlRouterProvider.otherwise(function($injector,$location){
        ... some advanced code...
    });
})

如何正确使用.when()(例如声明的顺序)请在这里查看(更多细节和示例)

> Angular UI-Router $urlRouterProvider .when not working *anymore*
> Angular UI-Router $urlRouterProvider .when not working when I click

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读