可以't使用URL导航到ui路由器状态

Can't navigate to ui-router state with URL

本文关键字:导航 ui 路由器 状态 URL 使用 可以      更新时间:2023-09-26

我正在使用ui路由器开发一个简单的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点击事件,该事件触发我的控制器中的以下功能:

 $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(如我在控制器中所示)以及在HTML模板中使用ui-sref directive(但不是直接导航到URL)。

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