如何$ state.go()

问题描述 投票:6回答:2

是我的$ stateProvider:

$stateProvider
.state('home', {
    url : '/',
    templateUrl : '/admindesktop/templates/main/',
    controller : 'AdminDesktopMainController'
}).state('company', {
    url : '/company',
    templateUrl : '/admindesktop/templates/grid/',
    controller : 'CompanyGridController'
}).state('company.detail', {
    url : '/{id:\d+}', //id is 
    templateUrl : '/admindesktop/templates/company/detail/',
    controller : 'CompanyDetailController'
});

它适用于'公司'状态(我使用ui-sref),但此代码不起作用(从'公司'状态调用):

$state.go('.detail', {
    id: $scope.selectedItem.id //selectedItem and id is defined
});

我从StackOverflow上阅读官方文档和答案,但我找不到解决方案。我不能使用ui-sref,我使用ui-grid,并在从表中选择一行进行编辑后打开新状态。

我做错了什么?

angularjs angular-ui-router
2个回答
16
投票

总是有效的是完整的状态定义:

// instead of this
// $state.go('.detail', {
// use this
$state.go('company.detail', {
    id: $scope.selectedItem.id //selectedItem and id is defined
});

doc there are defined these options,但他们依赖于CURRENT状态:

串起来 绝对州名或相对州路径。一些例子:

  • $ state.go('contact.detail') - 将转到contact.detail状态
  • $ state.go('^') - 将转到父状态
  • $ state.go('^ .sibling') - 将进入兄弟姐妹州
  • $ state.go('。child.grandchild') - 将去孙子州

1
投票

我的错误是正则表达式,真的:

.state('company.detail', {
    url : '/{id:\d*}',
    templateUrl : '/admindesktop/templates/company/detail/',
    controller : 'CompanyDetailController'
 })

{id:\ d *}有效(整数id)。

© www.soinside.com 2019 - 2024. All rights reserved.