angularjs - 如何在不首先导航到 index.html 的情况下直接进入带有 ui-router 的状态

标签 angularjs angular-ui-router

我有一个使用 ui-router 的 AngularJS 应用程序。一切正常,但我想要一个用户注册确认屏幕,如下所示:

  http://localhost:2757/Auth/confirmation

在我目前的配置中,当我使用此链接打开浏览器页面时,它不会查找 index.html 并且不会进入/Auth/confirmation 状态。我明白为什么会发生这种情况,但我不知道如何解决这个问题。

有人能给我一些关于如何/是否可以建立一个链接的建议 让我直接进入/Auth/confirmation .

我能想到的可能是这样的:
  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被 laoded - 作为 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
  • 关于angularjs - 如何在不首先导航到 index.html 的情况下直接进入带有 ui-router 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27958051/

    相关文章:

    angularjs - UI 路由器条件 ui View ?

    javascript - 我怎样才能控制台记录这个对象

    javascript - 具有.net核心模型的AngularJS未绑定(bind)

    javascript - 是否有一个 HTML 元素可以容纳 span 并且内部有可编辑的文本?

    javascript - 如何检查 angularjs 服务中的返回值是否是 promise ?

    javascript - 具有更改 ng-class 的 Ui-router 嵌套 View

    angularjs - 找不到任务 "serve"

    angularjs - Angular2 rc1 - @Routes 声明的顺序是否重要(可选参数)?

    javascript - 具有不同 $stateParams 的 Ionic $ionicHistory.goBack

    javascript - Angular ui-router 中的 fromState 返回空对象