否则在StateProvider上

Ade*_*lin 94 angularjs angular-ui angular-ui-router

使用angular-ui-router,如何在$ stateProvider上使用其他方法或者我该如何使用它?

小智 122

你不能只使用$stateProvider.

您需要注入$urlRouterProvider并创建类似于以下内容的代码:

$urlRouterProvider.otherwise('/otherwise');
Run Code Online (Sandbox Code Playgroud)

/otherwiseURL必须在国家像往常一样被定义:

 $stateProvider
    .state("otherwise", { url : '/otherwise'...})
Run Code Online (Sandbox Code Playgroud)

请参阅ksperling解释的链接


Jua*_*dez 81

您可以$stateProvider使用catch all语法("*path").您只需在列表底部添加状态配置,如下所示:

$stateProvider.state("otherwise", {
    url: "*path",
    templateUrl: "views/error-not-found.html"
});
Run Code Online (Sandbox Code Playgroud)

所有选项都在https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters中解释.

与此相反,此选项的好处$urlRouterProvider.otherwise(...)在于您不会被迫进行位置更改.


Zak*_*nry 34

您还可以手动将$ state注入其他函数,然后您可以导航到非url状态.这有利于浏览器不更改地址栏,这有助于处理返回上一页.

    $urlRouterProvider.otherwise(function ($injector, $location) {
        var $state = $injector.get('$state');

        $state.go('defaultLayout.error', {
            title: "Page not found",
            message: 'Could not find a state associated with url "'+$location.$$url+'"'
        });
    });
Run Code Online (Sandbox Code Playgroud)