问题描述
先前的解决方案 在0.2.12-我们可以使用的版本之前$urlRouterProvider.when(),在文档中建议使用(小引用):
如何:设置默认/索引子状态 … 如果您希望’parent.index’网址为非空,请使用$ urlRouterProvider在module.config中设置重定向:
$urlRouterProvider.when('/home', '/home/index');
var whenConfig = ['$urlRouterProvider', function($urlRouterProvider) {
$urlRouterProvider
.when('/app/list', ['$state', 'myService', function ($state, myService) {
$state.go('app.list.detail', {id: myService.Params.id});
}])
.otherwise('/app');
}];
...
app.config(whenConfig)
现在-我们不能。
UI路由器“ FIX”在 0.2.13 这是由于在版本0.2.13中提到的“ FIX” (我根本不确定)
Bug修复$状态: - ...... -从URL避免重新同步后.transitionTo(b267ecd3,关闭#1573)
if (lastPushedUrl && $location.url() === lastPushedUrl)
// this line stops the corrent .when to work
return lastPushedUrl = undefined;
lastPushedUrl = undefined;
这段代码正在优化/修复其他问题 ……而且,关闭了.when()功能
解 如前所述,这个全新的插件提供了0.2.13+版本的方法。我们只需要听状态更改,如果状态为“ app.list”,我们可以使用一些ID来详细了解它。
var onChangeConfig = ['$rootScope', '$state',
function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState) {
if (toState.name === "app.list") {
event.preventDefault();
$state.go('app.list.detail', {id: 2});
}
});
}]
解决方法
我.when('/center','/center/question')
在我的角度web应用程序。
当我'/center'
在浏览器中键入内容时,它会重定向到'/center/question'
我期望的位置,但是当我单击时<aui-sref="center" href="#/center"></a>
,它不会重定向,而是停留在url上'/center'
。
我的控制台没有错误,我也不知道为什么。
当不再工作时 我在这里看到了一个类似的问题AngularUI-Router$urlRouterProvider。我尝试了答案,但它仍然对我不起作用。
这是我的coffeescript代码:
whenConfig = ['$urlRouterProvider',($urlRouterProvider) ->
# default url config
$urlRouterProvider
.otherwise '/center'
.when '/center','/center/question'
]
stateConfig = ['$stateProvider',($stateProvider) ->
# nested url config
capitalize = (s)->
s[0].toUpperCase() + s[1..]
mainConfig = ({
name: name
url: "/#{name}"
templateUrl: "templates/#{name}.html"
controller: "#{capitalize name}Ctrl"
} for name in ['center','keywordList','keywordConfig','log','stat'])
centerConfig = ({
name: "center.#{name}"
url: "/#{name}?page"
templateUrl: "templates/center/#{name}.html"
controller: "Center#{capitalize name}Ctrl"
resolve:
thead: (CenterService) ->
CenterService.getThead @self.name
data: (CenterService,$stateParams) ->
CenterService.fetchItems @self.name,$stateParams.page
} for name in ['question','answer','comment','article'])
for singleConfig in mainConfig
$stateProvider.state singleConfig
for childConfig in centerConfig
$stateProvider.state childConfig
]
app.config whenConfig
app.config stateConfig