【问题标题】:Can't navigate to ui-router state with URL无法使用 URL 导航到 ui-router 状态
【发布时间】:2015-02-07 16:46:47
【问题描述】:

我正在使用 ui-router 开发一个简单的 Angular 应用程序。我有几个州可以选择然后编辑有关出版商的信息。相关配置:

            .state('select-publisher', {
                url: '/select-publisher',
                templateUrl: '/Content/superadmin/src/templates/publishers/publishers.html'
            })

            .state('publisher', {
                abstract: true,
                url: 'publisher/{id:int}',
                templateUrl: '/Content/superadmin/src/templates/publishers/publisher.html'
            })
            .state('publisher.details', {
                url: '/details',
                templateUrl: '/Content/superadmin/src/templates/publishers/details.html'
            })
            .state('publisher.ad-tags', {
                url: '/ad-tags',
                templateUrl: '/Content/superadmin/src/templates/publishers/ad-tags.html'
            })
            .state('publisher.native-ads', {
                url: '/native-ads',
                templateUrl: '/Content/superadmin/src/templates/publishers/native-ads.html'
            })

在选择发布者状态中,我有一个可用发布者的大列表。它们中的每一个都绑定到一个 ng-click 事件,该事件会在我的控制器中触发以下功能:

 $scope.selectPublisher = function(publisher) {
     publisherService.setSelectedPublisher(publisher);
     $state.go('publisher.details', {id: publisher.Id});
 };

这很好用,将我带到 publisher.details 状态并呈现正确的视图。此时,我浏览器中的 URL 指向 localhost:1337/superadmin#/publisher/39/details,其中 39 是我选择的发布者的 ID。

问题是,如果我刷新此页面或尝试通过将 URL 从应用程序的另一个区域粘贴到浏览器中直接导航到该页面,我总是会被带回 select-publisher 状态。我希望能够配置我的状态,以便我能够根据 URL 导航到详细信息状态(或任何其他状态)。

值得注意的是,在我的所有状态之后,我确实定义了一条捕获所有路线:

$urlRouterProvider.otherwise('/select-publisher');

我假设由于某种原因触发了此操作,但我无法解释为什么导航可以在我的应用程序中使用 $state.go(正如我在控制器中指出的那样)以及在我的控制器中使用 ui-sref directive HTML 模板,但不是通过直接导航到 URL。

【问题讨论】:

  • 您的完整网址是什么样的?
  • 问题里已经发过了
  • 可能是因为缺少斜线url: '/publisher/{id:int}'
  • @lujcon 哇...谢谢伙计,我不敢相信我没有发现这一点。有时你只需要第二双眼睛。如果您将其添加为答案,我会将其标记为正确。
  • 我不是来收集积分的 ;) 我很高兴能提供帮助。

标签: javascript angularjs angular-ui-router


【解决方案1】:

可能是因为缺少斜杠网址:/publisher/{id:int}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2018-01-27
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    相关资源
    最近更新 更多