使用angular-ui-router,如何使用otherwise method$stateProvider或者我怎样才能使用它?其他在StateProvider上
回答
您只能使用$stateProvider
。
你需要注入$urlRouterProvider
,并创建类似代码:
$urlRouterProvider.otherwise('/otherwise');
的/otherwise
url必须的状态像往常一样被定义为:
$stateProvider
.state("otherwise", { url : '/otherwise'...})
看到这个链接,ksperling explains
我试过了,它工作!因此这是最好的答案;) – Adelin
你实际上可以使用'$ stateProvider'。如果您只想显示模板,但不重定向,则工作量会减少。我更喜欢@ juan-hernandez的回答。 – weltschmerz
传递给'否则'的值(在这种情况下''/ otherwise''')必须匹配状态的名称(第一个参数到'.state')或'url'选项的值? – d512
好的,当你意识到你问的问题已经在示例代码的第一行中得到了回答的时候,这个愚蠢的时刻!看看示例代码。
angular.module('sample', ['ui.compat'])
.config(
[ '$stateProvider', '$routeProvider', '$urlRouterProvider',
function ($stateProvider, $routeProvider, $urlRouterProvider) {
$urlRouterProvider
.when('/c?id', '/contacts/:id')
.otherwise('/'); // <-- This is what I was looking for !
....
您可以使用$stateProvider
的捕获所有的语法("*path"
)。你只需要在列表的底部添加的状态配置类似以下:
$stateProvider.state("otherwise", {
url: "*path",
templateUrl: "views/error-not-found.html"
});
所有的选项都在https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters解释。
与$urlRouterProvider.otherwise(...)
相比,此选项的好处在于您不必强制改变位置。
请你详细说明一下:“你不是被迫改变位置”吗? –
他的意思是网址将保持原样。因此,如果用户转到“/ this/does/not/exist”,那么URL将保持在地址栏中。另一种解决方案将带你到'/否' –
我用你的解决方案,它的工作(我可以保持没有找到的网址,这是伟大的,因为我使用http://luisfarzati.github.io/angulartics/和这种方式,我也可以看到导航到网页没有找到)的情况下,用户浏览的网址不存在。但是,如果我在控制器内部使用$ state.go('otherwise')导航到此URL,则会丢失该URL。当用户转到项目的详细信息页面并且服务器返回404时(例如,如果该项目被删除),我明确导航到此状态。你知道解决这个问题的方法吗? – pcatre
您也可以手动将$ 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+'"'
});
});
的accepted answer引用angular-route
询问ui-router
。该接受的答案使用"monolithic"$routeProvider
,这就要求ngRoute
模块(而ui-router
需要ui.router
模块)
的highest-rated answer使用$stateProvider
代替,并说像.state("otherwise", { url : '/otherwise'... })
, 但我找不到任何提及“否则”在documentation it links。但是,我看到它说,$stateProvider
提到in this answer:
你不仅可以使用
$stateProvider
。您需要注入$urlRouterProvider
这就是我的答案可能对您有帮助的地方。对我来说,这是足够使用$urlRouterProvider
就像这样:
my_module
.config([
, '$urlRouterProvider'
, function(
, $urlRouterProvider){
//When the url is empty; i.e. what I consider to be "the default"
//Then send the user to whatever state is served at the URL '/starting'
//(It could say '/default' or any other path you want)
$urlRouterProvider
.when('', '/starting');
//...
}]);
我的建议是与ui-router
documentation,(this specific revision),它包括一个类似用途的一致。when(...)
方法(第一次调用函数):
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');
})
- 1. StateProvider在AngularJS的其他地方比app.js
- 2. stateprovider在手机
- 3. app.config + stateProvider
- 4. 在其他平台上
- 5. wakanda angular $ urlRouterProvider stateProvider
- 6. $ stateprovider multiple routes angularjs
- 7. AngularJS $ stateProvider速记
- 8. $ stateProvider不工作
- 9. 添加$ stateProvider
- 10. 到$ routeProvider或$ stateProvider
- 11. blogspot.com上其他域
- 12. 渲染基础上,决心$ stateProvider模板
- 13. IE8和$ stateProvider问题
- 14. $ stateProvider angularJs不工作
- 15. $ stateProvider params null with ionic
- 16. 在UWAMP上安装ffmpeg和其他人
- 17. 在其他窗口之上的窗口
- 18. 子元素在其他元素之上
- 19. 在OSX上运行其他java版本
- 20. 在其他服务器上运行mysqlbinlog
- 21. 在AWS上编译其他语言Lambda
- 22. Jar在其他PC上无法运行
- 23. 在其他虚拟机上请求API
- 24. HTML5(或其他)在窗口上M3U8
- 25. 在其他电脑上运行Allegro 5
- 26. NSTextFields在其他Mac上位置较低
- 27. 在其他课上更改tkinter标签?
- 28. php在其他页面上取值
- 29. .bind animationend在其他函数上运行
- 30. EAGLView在其他视图上的性能
你想要做什么? (请注意,我不完全理解'$ stateProvider.otherwise',所以它会帮助我) –